Fly diversity

I’ve done quite a few different things, just trying to work out the best way to use it. I would say that it seems fairly consistent in the patterns between my ‘all in analysis’ and ‘only day 1’ analysis. Which I think is good. Apple, pear and kiwifruit suffer a bit later on when it comes to having samples with only 0’s for non bee species or only one species which makes MDS plots etc imposible to converge so they are filtered in and out throughout the analysis.

##############
#data sorting#
##############

#basic diversity stats + MDS
#means I need to deal with the naming issues on species ID
#sorting data feel free to ignore its just getting it into a format I can use

#setwd("C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024")

apple<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Apple')
pear<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Pear')
avocado<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Avocado')
kiwifruit<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Kiwifruit')
pakchoi<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Pak choi')
radish<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Radish')
onion<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Onion')
carrot<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Daily insect counts all crops for Eddy_mod.xlsx',sheet='Carrot')
#brad wants them grouped on the column labeled 'Ordering'
#scientific name is wacky so stick to the ordering column as the grouping variable
#remove “Ephydridae spp.” from Pak choi and onion data
#as per brad is wasnt counted in the other crops so needs to be removed
onion<-onion %>% filter(`Scientific name`!='Ephydridae spp.')
pakchoi<-pakchoi %>% filter(`Scientific name`!='Ephydridae spp.')

################
#data wrangling#
################

#sorting out daily counts as diversity statistics will be based on daily counts:
###apple
#day 1, 2, 3
apple_day1<-apple %>% select(-`Day 2`,-`Day 3`) %>% mutate(Day='Day_1') %>% filter(`Day 1`>0) %>% rename(raw_count = `Day 1`)
apple_day2<-apple %>% select(-`Day 1`,-`Day 3`) %>% mutate(Day='Day_2') %>% filter(`Day 2`>0) %>% rename(raw_count = `Day 2`)
apple_day3<-apple %>% select(-`Day 1`,-`Day 2`) %>% mutate(Day='Day_3') %>% filter(`Day 3`>0) %>% rename(raw_count = `Day 3`)

apple_day<-rbind(apple_day1,apple_day2,apple_day3)  

#just make sure everything is tidy tidy 
apple_fly<-apple_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Apple_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Ordering,Region,`Location and season`,Day,raw_count,Crop) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-apple_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Apple_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

apple_fly<-full_join(apple_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
###pear
#day 1, 2, 3
pear_day1<-pear %>% select(-`Day 2`,-`Day 3`) %>% mutate(Day='Day_1') %>% filter(`Day 1`>0) %>% rename(raw_count = `Day 1`)
pear_day2<-pear %>% select(-`Day 1`,-`Day 3`) %>% mutate(Day='Day_2') %>% filter(`Day 2`>0) %>% rename(raw_count = `Day 2`)
pear_day3<-pear %>% select(-`Day 1`,-`Day 2`) %>% mutate(Day='Day_3') %>% filter(`Day 3`>0) %>% rename(raw_count = `Day 3`)

pear_day<-rbind(pear_day1,pear_day2,pear_day3) 

#just make sure everything is tidy tidy 
pear_fly<-pear_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Pear_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Ordering,Region,`Location and season`,Day,raw_count,Crop) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-pear_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Pear_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

pear_fly<-full_join(pear_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
###avocado
#day 1, 2, 3
avocado_day1<-avocado %>% select(-`Day 2`,-`Day 3`) %>% mutate(Day='Day_1') %>% filter(`Day 1`>0) %>% rename(raw_count = `Day 1`)
avocado_day2<-avocado %>% select(-`Day 1`,-`Day 3`) %>% mutate(Day='Day_2') %>% filter(`Day 2`>0) %>% rename(raw_count = `Day 2`)
avocado_day3<-avocado %>% select(-`Day 1`,-`Day 2`) %>% mutate(Day='Day_3') %>% filter(`Day 3`>0) %>% rename(raw_count = `Day 3`)

avocado_day<-rbind(avocado_day1,avocado_day2,avocado_day3) 
#just make sure everything is tidy tidy 
avocado_fly<-avocado_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Avocado_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Ordering,Region,`Location and season`,Day,raw_count,Crop) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-avocado_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Avocado_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

avocado_fly<-full_join(avocado_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
###kiwifruit
#day 1, 2, 3
kiwifruit_day1<-kiwifruit %>% select(-`Day 2`,-`Day 3`) %>% mutate(Day='Day_1') %>% filter(`Day 1`>0) %>% rename(raw_count = `Day 1`)
kiwifruit_day2<-kiwifruit %>% select(-`Day 1`,-`Day 3`) %>% mutate(Day='Day_2') %>% filter(`Day 2`>0) %>% rename(raw_count = `Day 2`)
kiwifruit_day3<-kiwifruit %>% select(-`Day 1`,-`Day 2`) %>% mutate(Day='Day_3') %>% filter(`Day 3`>0) %>% rename(raw_count = `Day 3`)

kiwifruit_day<-rbind(kiwifruit_day1,kiwifruit_day2,kiwifruit_day3) 

#just make sure everything is tidy tidy 
kiwifruit_fly<-kiwifruit_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Kiwifruit_',Region,'_',`Location and season`,'_',Day)) %>% select(Crop,sample_ID,Ordering,Region,`Location and season`,Day,raw_count) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-kiwifruit_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Kiwifruit_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

kiwifruit_fly<-full_join(kiwifruit_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
###pakchoi
#day 1
pakchoi_day1<-pakchoi %>% mutate(Day='Day_1') %>% rename(raw_count = `Corrected Daily Count`)

pakchoi_day<-pakchoi_day1 

#just make sure everything is tidy tidy 
pakchoi_fly<-pakchoi_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Pakchoi_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Ordering,Region,`Location and season`,Day,raw_count,Crop) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-pakchoi_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Pakchoi_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

pakchoi_fly<-full_join(pakchoi_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
###radish
#day 1
radish_day1<-radish %>% mutate(Day='Day_1') %>% rename(raw_count = `Corrected Daily Count`)

radish_day<-radish_day1 

#just make sure everything is tidy tidy 
radish_fly<-radish_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Radish_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Ordering,Region,`Location and season`,Day,raw_count,Crop) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-radish_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Radish_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

radish_fly<-full_join(radish_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
###onion
#day 1
onion_day1<-onion %>% mutate(Day='Day_1') %>% rename(raw_count = `Corrected Daily Count`)

onion_day<-onion_day1 

#just make sure everything is tidy tidy 
onion_fly<-onion_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Onion_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Ordering,Region,`Location and season`,Day,raw_count,Crop) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-onion_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Onion_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

onion_fly<-full_join(onion_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
###carrot
#day 1 2 3
carrot_day1<-carrot %>% select(-`Day 2`,-`Day 3`) %>% mutate(Day='Day_1') %>% filter(`Day 1`>0) %>% rename(raw_count = `Day 1`)
carrot_day2<-carrot %>% select(-`Day 1`,-`Day 3`) %>% mutate(Day='Day_2') %>% filter(`Day 2`>0) %>% rename(raw_count = `Day 2`)
carrot_day3<-carrot %>% select(-`Day 1`,-`Day 2`) %>% mutate(Day='Day_3') %>% filter(`Day 3`>0) %>% rename(raw_count = `Day 3`)

carrot_day<-rbind(carrot_day1,carrot_day2,carrot_day3) 
#just make sure everything is tidy tidy 
carrot_fly<-carrot_day%>% filter(Grouping!='Bees')%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Carrot_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Ordering,Region,`Location and season`,Day,raw_count,Crop) %>% group_by(sample_ID,Ordering,Region,`Location and season`,Day,Crop) %>% summarise(raw_count=sum(raw_count)) %>% ungroup()
## `summarise()` has grouped output by 'sample_ID', 'Ordering', 'Region',
## 'Location and season', 'Day'. You can override using the `.groups` argument.
#some replicates have only 0 counts for non bees and are being dropped  so need to be added back in
samples<-carrot_day%>% mutate(Ordering = factor(Ordering)) %>% mutate(sample_ID = paste0('Carrot_',Region,'_',`Location and season`,'_',Day)) %>% select(sample_ID,Region,`Location and season`,Day,Crop) %>% unique()

carrot_fly<-full_join(carrot_fly,samples)
## Joining with `by = join_by(sample_ID, Region, `Location and season`, Day,
## Crop)`
##############################
#generating tables and counts#
##############################

allcrops_per_r_fly<-rbind(apple_fly,pear_fly,avocado_fly,kiwifruit_fly,pakchoi_fly,radish_fly,onion_fly,carrot_fly)  

unique(allcrops_per_r_fly$Crop)
## [1] "Apple"     "Pear"      "Avocado"   "Kiwifruit" "Pak Choi"  "Radish"   
## [7] "Onion"     "Carrot"
counts<-allcrops_per_r_fly %>% ungroup() %>% select(sample_ID,Ordering,raw_count)

#I need to fill up the NA's for the samples with 0 flies with some dummy 0 for ones species to keep them in the next stage - so this is just to add a 0 value in and I've chosed the insect '27' at random (cause i know it occurs in the dataset so Im not adding something not present)

#in '' as its a factor not a number
counts$Ordering[is.na(counts$Ordering)] <- '27'
counts$raw_count[is.na(counts$raw_count)] <- 0
#transform wide
counts_wide<-counts %>% pivot_wider(names_from=`Ordering`,values_from = `raw_count`)
counts_wide[is.na(counts_wide)] <- 0
counts_wide<-counts_wide %>% remove_rownames %>% column_to_rownames(var="sample_ID")

#setting up meta data sheet for anova against variables, species richness vs crops
meta_data<-allcrops_per_r_fly %>% ungroup() %>% select(sample_ID,Crop,Region,`Location and season`,Day) %>% unique()

#adding in: spring vs summer flowering to metadata table
head(meta_data)
## # A tibble: 6 × 5
##   sample_ID                             Crop  Region `Location and season` Day  
##   <chr>                                 <chr> <chr>  <chr>                 <chr>
## 1 Apple_Hawkes Bay_104 Moteo-Pa road _… Apple Hawke… 104 Moteo-Pa road _2… Day_1
## 2 Apple_Hawkes Bay_104 Moteo-Pa road _… Apple Hawke… 104 Moteo-Pa road _2… Day_3
## 3 Apple_Hawkes Bay_185 Lawn road, Hast… Apple Hawke… 185 Lawn road, Hasti… Day_1
## 4 Apple_Hawkes Bay_185 Lawn road, Hast… Apple Hawke… 185 Lawn road, Hasti… Day_2
## 5 Apple_Hawkes Bay_185 Lawn road, Hast… Apple Hawke… 185 Lawn road, Hasti… Day_3
## 6 Apple_Hawkes Bay_2080 Pakawhai Road_… Apple Hawke… 2080 Pakawhai Road_2… Day_1
meta_data<-meta_data %>% mutate(flowertime = case_when(Crop == 'Apple' ~ "Spring",Crop == 'Pear' ~ "Spring",Crop == 'Avocado' ~ "Spring",Crop == 'Kiwifruit' ~ "Spring",Crop == 'Carrot' ~ "Summer",Crop == 'Radish' ~ "Summer",Crop == 'Onion' ~ "Summer",Crop == 'Pak Choi' ~ "Summer"))

#regrouping regions
meta_data$Region_original<-meta_data$Region
meta_data<-meta_data %>% mutate(Region = case_when(Region_original == 'Ashburton' ~ "Canterbury", Region_original == 'Darfield-Kirwee' ~ "Canterbury",Region_original == 'North Waimakariri' ~ "Canterbury",Region_original == 'Leeston' ~ "Canterbury",Region_original == 'Methven' ~ "Canterbury",Region_original == 'Waipara' ~ "Canterbury",Region_original == 'Kaitaia' ~ "Northland",Region_original == 'Whangarei' ~ "Whangarei",Region_original == 'Wairarapa' ~ "Wairarapa",Region_original == 'Bay of Plenty' ~ "Bay of Plenty",Region_original == 'Hawkes Bay' ~ "Hawkes Bay",Region_original == 'Motueka' ~ "Tasman",Region_original == 'Nelson' ~ "Tasman",Region_original == 'Takaka' ~ "Tasman",Region_original == 'Marlborough' ~ "Marlborough",Region_original == 'Gisborne' ~ "Poverty Bay",Region_original == 'Central Otago' ~ "Otago",Region_original == 'Southland' ~ "Southland",Region_original == 'Auckland' ~ "Auckland",Region_original == 'Canterbury' ~ "Canterbury"))
#unique(meta_data$Region)
#meta_data %>% select(Region, Region_original) %>% unique()

Network plots

These are based on average counts per day across all replicates

#need to calculate average number of counts per tree per sample
#head(allcrops_per_r_fly)
allcrops_summarisedcounts<-allcrops_per_r_fly %>% select(sample_ID,Ordering,Crop,raw_count) %>% group_by(Crop,Ordering) %>% summarise(total_insects=sum(raw_count))
## `summarise()` has grouped output by 'Crop'. You can override using the
## `.groups` argument.
sample_count<-allcrops_per_r_fly %>% select(sample_ID,Crop) %>% group_by(Crop) %>% summarise(sample_per_crop=n_distinct(sample_ID))
allcrops_summarisedcounts<-left_join(allcrops_summarisedcounts,sample_count) %>% mutate(average_count=total_insects/sample_per_crop)
## Joining with `by = join_by(Crop)`
#remove those NAs that were added in for the 0's
allcrops_summarisedcounts <- na.omit(allcrops_summarisedcounts)

brads_col_2<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Copy of fly Pie chart data for Eddyv2_flies.xlsx',sheet='Insect_ordering')
head(brads_col_2)
## # A tibble: 6 × 3
##    Code Species                   Colour
##   <dbl> <chr>                     <chr> 
## 1     1 Apis mellifera            gray94
## 2     2 Bombus terrestris         gray85
## 3     3 Bombus ruderatus/hortorum gray85
## 4     4 Megachile rotundata       gray85
## 5     5 Lasioglossum sordidum     gray85
## 6     6 Leioproctus spp.          gray85
brads_col_2$color_hex<-col2hex(brads_col_2$Colour)
nodes_notthebees<-data.frame(name=c(as.character(allcrops_summarisedcounts$Ordering),as.character((allcrops_summarisedcounts$Crop))) %>% unique())

allcrops_summarisedcounts$IDsource <- match(allcrops_summarisedcounts$Ordering, nodes_notthebees$name)-1 
allcrops_summarisedcounts$IDtarget <- match(allcrops_summarisedcounts$Crop, nodes_notthebees$name)-1

node_col<-nodes_notthebees
brads_col_2$Code<-as.character(brads_col_2$Code)
brads_col_2_cut <-brads_col_2 %>% select(Code,color_hex) %>% unique() %>% na.omit()
node_col<-left_join(node_col,brads_col_2_cut,by=c('name'='Code'))
#add colours for crops
col_bound<-rcartocolor::carto_pal(n=9,name='Prism')

node_col<-node_col %>% mutate(color_hex = case_when(name == 'Apple' ~ "#5F4690",name == 'Avocado' ~ "#1D6996",name == 'Carrot' ~ "#38A6A5",name == 'Kiwifruit' ~ "#0F8554",name == 'Onion' ~ "#73AF48",name == 'Pak Choi' ~ "#EDAD08",name == 'Pear' ~ "#E17C05",name == 'Radish' ~ "#CC503E",
    TRUE ~ color_hex))
#brads new colours
node_col<-node_col %>% mutate(color_hex = case_when(name == 'Apple' ~ "#007b83",name == 'Avocado' ~ "#6e7d3c",name == 'Carrot' ~ "#CD951C",name == 'Kiwifruit' ~ "#b5a53c",name == 'Onion' ~ "#6baed6",name == 'Pak Choi' ~ "#E0C591",name == 'Pear' ~ "#b78080",name == 'Radish' ~ "#516295",
    TRUE ~ color_hex))
#col_bound<-c("#6e7d3c", "#007b83","#b78080","#b5a53c","#E0C591","#516295","#CD951C","#b4daeb")

allcrops_summarisedcounts<-left_join(allcrops_summarisedcounts,node_col,by=c('Ordering'='name'))

plot_ly(
  type = "sankey",
  orientation = "h",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 20,
              line = list(color = "black", width = 0.5),
              label = node_col$name,
              color=node_col$color_hex
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              color=allcrops_summarisedcounts$color_hex),
  textfont = list(size = 10),
  width = 700,
  height = 500
) %>%
  layout(title = "Sankey Diagram",
         font = list(size = 14),
         margin = list(t = 40, l = 20, r = 10, b = 10))
#o set opacity I need to switch to rgba format rather than hex
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% rowwise() %>% mutate(rgba=paste0('rgba(',paste(col2rgb(color_hex),collapse=','),',0.5)')) 



plot_ly(
  type = "sankey",
  arrangement='snap',
  #domain =list(
  # x=c(0,1),
  #  y=c(0,1)
  #),
  orientation = "h",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 15,
              line = list(color = "black", width = 0.5),
              label = as.factor(node_col$name),
              color=node_col$color_hex
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              #     value = test_merge_ave6trees$sum_6_tree),
              color=allcrops_summarisedcounts$rgba
  )
) %>%
  layout(title = "Sankey Diagram",
         font = list(size = 10),
         xaxis = list(showgrid=F,zeroline=F),
         yaxis=list(showgrid=F,zeroline=F))
#brad wants crops at the top (just to be difficult)
allcrops_summarisedcounts<-allcrops_per_r_fly %>% select(sample_ID,Ordering,Crop,raw_count) %>% group_by(Crop,Ordering) %>% summarise(total_insects=sum(raw_count))
## `summarise()` has grouped output by 'Crop'. You can override using the
## `.groups` argument.
sample_count<-allcrops_per_r_fly %>% select(sample_ID,Crop) %>% group_by(Crop) %>% summarise(sample_per_crop=n_distinct(sample_ID))
allcrops_summarisedcounts<-left_join(allcrops_summarisedcounts,sample_count) %>% mutate(average_count=total_insects/sample_per_crop)
## Joining with `by = join_by(Crop)`
#remove those NAs that were added in for the 0's
allcrops_summarisedcounts <- na.omit(allcrops_summarisedcounts)

brads_col_2<-read_excel('C:/Users/hrlexd/Dropbox/PlantAndFood (1)/B4BI/Review_paper2024/Copy of fly Pie chart data for Eddyv2_flies.xlsx',sheet='Insect_ordering')
head(brads_col_2)
## # A tibble: 6 × 3
##    Code Species                   Colour
##   <dbl> <chr>                     <chr> 
## 1     1 Apis mellifera            gray94
## 2     2 Bombus terrestris         gray85
## 3     3 Bombus ruderatus/hortorum gray85
## 4     4 Megachile rotundata       gray85
## 5     5 Lasioglossum sordidum     gray85
## 6     6 Leioproctus spp.          gray85
brads_col_2$color_hex<-col2hex(brads_col_2$Colour)
nodes_notthebees<-data.frame(name=c(as.character(allcrops_summarisedcounts$Ordering),as.character((allcrops_summarisedcounts$Crop))) %>% unique())

allcrops_summarisedcounts$IDsource <- match(allcrops_summarisedcounts$Crop, nodes_notthebees$name)-1
  
allcrops_summarisedcounts$IDtarget <- match(allcrops_summarisedcounts$Ordering, nodes_notthebees$name)-1 
node_col<-nodes_notthebees
brads_col_2$Code<-as.character(brads_col_2$Code)
brads_col_2_cut <-brads_col_2 %>% select(Code,color_hex) %>% unique() %>% na.omit()
node_col<-left_join(node_col,brads_col_2_cut,by=c('name'='Code'))
#add colours for crops
col_bound<-rcartocolor::carto_pal(n=9,name='Prism')

node_col<-node_col %>% mutate(color_hex = case_when(name == 'Apple' ~ "#007b83",name == 'Avocado' ~ "#6e7d3c",name == 'Carrot' ~ "#CD951C",name == 'Kiwifruit' ~ "#b5a53c",name == 'Onion' ~ "#6baed6",name == 'Pak Choi' ~ "#E0C591",name == 'Pear' ~ "#b78080",name == 'Radish' ~ "#516295",
    TRUE ~ color_hex))

allcrops_summarisedcounts<-left_join(allcrops_summarisedcounts,node_col,by=c('Ordering'='name'))

plot_ly(
  type = "sankey",
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 20,
              line = list(color = "black", width = 0.5),
              label = node_col$name,
              color=node_col$color_hex
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              color=allcrops_summarisedcounts$color_hex),
  textfont = list(size = 10),
  width = 700,
  height = 500
) %>%
  layout(font = list(size = 14),
         margin = list(t = 40, l = 20, r = 10, b = 10))
#o set opacity I need to switch to rgba format rather than hex
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% rowwise() %>% mutate(rgba=paste0('rgba(',paste(col2rgb(color_hex),collapse=','),',0.5)')) 



plot_ly(
  type = "sankey",
  arrangement='snap',
  #domain =list(
  # x=c(0,1),
  #  y=c(0,1)
  #),
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 15,
              line = list(color = "black", width = 0.5),
              label = as.factor(node_col$name),
              color=node_col$color_hex
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              #     value = test_merge_ave6trees$sum_6_tree),
              color=allcrops_summarisedcounts$rgba
  )
) %>%
  layout(font = list(size = 10),
         xaxis = list(showgrid=F,zeroline=F),
         yaxis=list(showgrid=F,zeroline=F))
#talked about changing the colours schemes to things just done for SVD as well
#or only the top 10 count sets
#or intersect of both

#top 10 count sets
head(allcrops_summarisedcounts)
## # A tibble: 6 × 9
## # Rowwise:  Crop
##   Crop  Ordering total_insects sample_per_crop average_count IDsource IDtarget
##   <chr> <chr>            <dbl>           <int>         <dbl>    <dbl>    <dbl>
## 1 Apple 9                    1              67        0.0149       67        0
## 2 Apple 13                   6              67        0.0896       67        1
## 3 Apple 14                  49              67        0.731        67        2
## 4 Apple 16                   5              67        0.0746       67        3
## 5 Apple 23                   3              67        0.0448       67        4
## 6 Apple 24                   2              67        0.0299       67        5
## # ℹ 2 more variables: color_hex <chr>, rgba <chr>
top_10_taxa<-allcrops_summarisedcounts %>% select(Ordering, average_count) %>% group_by(Ordering) %>% summarise(totalsum=sum(average_count)) %>% arrange(desc(totalsum)) %>% slice_head(n = 10)
## Adding missing grouping variables: `Crop`
unique(node_col$name)
##  [1] "9"         "13"        "14"        "16"        "23"        "24"       
##  [7] "25"        "32"        "33"        "34"        "38"        "51"       
## [13] "52"        "54"        "55"        "56"        "65"        "11"       
## [19] "15"        "22"        "26"        "40"        "47"        "10"       
## [25] "12"        "17"        "18"        "19"        "21"        "27"       
## [31] "31"        "36"        "37"        "41"        "42"        "43"       
## [37] "44"        "46"        "48"        "57"        "61"        "63"       
## [43] "67"        "73"        "74"        "76"        "29"        "30"       
## [49] "53"        "58"        "66"        "72"        "62"        "71"       
## [55] "49"        "50"        "39"        "28"        "70"        "77"       
## [61] "59"        "60"        "68"        "69"        "75"        "78"       
## [67] "20"        "Apple"     "Avocado"   "Carrot"    "Kiwifruit" "Onion"    
## [73] "Pak Choi"  "Pear"      "Radish"
top_10_taxa<-c(top_10_taxa$Ordering,"Apple","Avocado","Carrot","Kiwifruit","Onion","Pak Choi","Pear","Radish")
#taxa that have SVD data associated with them are:
#calliphora stygia: 13
#calliphora vicina: 14
#lucilla sericata: 15
#pollenia psudorudis: 19
#pollenia spp.: 20
#hydrotaea rostrata: 22
#delia platura: 24 #bit of a shit pollinator in terms of SVD
#oxysarcodexia varia: 26
#prophystricia alcis: 27
#eristalis tenax: 32
#melangyna novaezelandiae: 33
#melanostoma fasciatum: 34
#helophilus hochstelleri: 36
#helophilus cingulatus: 37
#allographta dorsalis: 39
#odontomyia spp: 43
#dilophus nigrostigma: 47
#zorion guttigerum: 55 #bit of a shit pollinator in terms of SVD
svd_taxa<-c('13','14','15','19','20','22','24','26','27','32','33','34','36','37','39','43','47','55',"Apple","Avocado","Carrot","Kiwifruit","Onion","Pak Choi","Pear","Radish")

#top 10
node_col<-node_col %>% mutate(col_top10=case_when(name %in% top_10_taxa ~ color_hex, TRUE ~ '#D3D3D3'))
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% mutate(col_top10=case_when(Ordering %in% top_10_taxa ~ color_hex, TRUE ~ '#D3D3D3'))

plot_ly(
  type = "sankey",
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 20,
              line = list(color = "black", width = 0.5),
              label = node_col$name,
              color=node_col$col_top10
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              color=allcrops_summarisedcounts$col_top10),
  textfont = list(size = 10),
  width = 700,
  height = 500
) %>%
  layout(font = list(size = 14),
         margin = list(t = 40, l = 20, r = 10, b = 10))
#to set grey opacity I need to switch to rgba format rather than hex
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% rowwise() %>% mutate(rgba_top10=paste0('rgba(',paste(col2rgb(col_top10),collapse=','),',1)')) 

allcrops_summarisedcounts$rgba_top10<-gsub('rgba(211,211,211,1)','rgba(211,211,211,0.5)',allcrops_summarisedcounts$rgba_top10, fixed = TRUE)

plot_ly(
  type = "sankey",
  arrangement='snap',
  #domain =list(
  # x=c(0,1),
  #  y=c(0,1)
  #),
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 15,
              line = list(color = "black", width = 0.5),
              label = as.factor(node_col$name),
              color=node_col$col_top10
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              #     value = test_merge_ave6trees$sum_6_tree),
              color=allcrops_summarisedcounts$rgba_top10
  )
) %>%
  layout(font = list(size = 10),
         xaxis = list(showgrid=F,zeroline=F),
         yaxis=list(showgrid=F,zeroline=F))
#things that we have SVD for:

#svd
node_col<-node_col %>% mutate(col_svd=case_when(name %in% svd_taxa ~ color_hex, TRUE ~ '#D3D3D3'))
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% mutate(col_svd=case_when(Ordering %in% svd_taxa ~ color_hex, TRUE ~ '#D3D3D3'))


plot_ly(
  type = "sankey",
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 20,
              line = list(color = "black", width = 0.5),
              label = node_col$name,
              color=node_col$col_svd
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              color=allcrops_summarisedcounts$col_svd),
  textfont = list(size = 10),
  width = 700,
  height = 500
) %>%
  layout(font = list(size = 14),
         margin = list(t = 40, l = 20, r = 10, b = 10))
#to set grey opacity I need to switch to rgba format rather than hex
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% rowwise() %>% mutate(rgba_svd=paste0('rgba(',paste(col2rgb(col_svd),collapse=','),',1)')) 

allcrops_summarisedcounts$rgba_svd<-gsub('rgba(211,211,211,1)','rgba(211,211,211,0.5)',allcrops_summarisedcounts$rgba_svd, fixed = TRUE)

plot_ly(
  type = "sankey",
  arrangement='snap',
  #domain =list(
  # x=c(0,1),
  #  y=c(0,1)
  #),
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 15,
              line = list(color = "black", width = 0.5),
              label = as.factor(node_col$name),
              color=node_col$col_svd
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              #     value = test_merge_ave6trees$sum_6_tree),
              color=allcrops_summarisedcounts$rgba_svd
  )
) %>%
  layout(font = list(size = 10),
         xaxis = list(showgrid=F,zeroline=F),
         yaxis=list(showgrid=F,zeroline=F))
#intersect of both while ignoring two that didnt have good svd
top_10_taxa_svd<- top_10_taxa[top_10_taxa %in% svd_taxa]

node_col<-node_col %>% mutate(col_top10_svd=case_when(name %in% top_10_taxa_svd ~ color_hex, TRUE ~ '#D3D3D3'))
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% mutate(col_top10_svd=case_when(Ordering %in% top_10_taxa_svd ~ color_hex, TRUE ~ '#D3D3D3'))


plot_ly(
  type = "sankey",
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 20,
              line = list(color = "black", width = 0.5),
              label = node_col$name,
              color=node_col$col_top10_svd
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              color=allcrops_summarisedcounts$col_top10_svd),
  textfont = list(size = 10),
  width = 700,
  height = 500
) %>%
  layout(font = list(size = 14),
         margin = list(t = 40, l = 20, r = 10, b = 10))
#to set grey opacity I need to switch to rgba format rather than hex
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% rowwise() %>% mutate(rgba_top10_svd=paste0('rgba(',paste(col2rgb(col_top10_svd),collapse=','),',1)')) 

allcrops_summarisedcounts$rgba_top10_svd<-gsub('rgba(211,211,211,1)','rgba(211,211,211,0.5)',allcrops_summarisedcounts$rgba_top10_svd, fixed = TRUE)

plot_ly(
  type = "sankey",
  arrangement='snap',
  #domain =list(
  # x=c(0,1),
  #  y=c(0,1)
  #),
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 15,
              line = list(color = "black", width = 0.5),
              label = as.factor(node_col$name),
              color=node_col$col_top10_svd
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              #     value = test_merge_ave6trees$sum_6_tree),
              color=allcrops_summarisedcounts$rgba_top10_svd
  )
) %>%
  layout(font = list(size = 10),
         xaxis = list(showgrid=F,zeroline=F),
         yaxis=list(showgrid=F,zeroline=F))
#run without 24 as that one is a bit shit on SVD
top_10_taxa_svd_fil <- top_10_taxa_svd[!top_10_taxa_svd %in% c('24')]

node_col<-node_col %>% mutate(col_top10_svd_fil=case_when(name %in% top_10_taxa_svd_fil ~ color_hex, TRUE ~ '#D3D3D3'))
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% mutate(col_top10_svd_fil=case_when(Ordering %in% top_10_taxa_svd_fil ~ color_hex, TRUE ~ '#D3D3D3'))


plot_ly(
  type = "sankey",
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 20,
              line = list(color = "black", width = 0.5),
              label = node_col$name,
              color=node_col$col_top10_svd_fil
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              color=allcrops_summarisedcounts$col_top10_svd_fil),
  textfont = list(size = 10),
  width = 700,
  height = 500
) %>%
  layout(font = list(size = 14),
         margin = list(t = 40, l = 20, r = 10, b = 10))
#to set grey opacity I need to switch to rgba format rather than hex
allcrops_summarisedcounts<-allcrops_summarisedcounts %>% rowwise() %>% mutate(rgba_top10_svd_fil=paste0('rgba(',paste(col2rgb(col_top10_svd_fil),collapse=','),',1)')) 

allcrops_summarisedcounts$rgba_top10_svd_fil<-gsub('rgba(211,211,211,1)','rgba(211,211,211,0.5)',allcrops_summarisedcounts$rgba_top10_svd_fil, fixed = TRUE)

plot_ly(
  type = "sankey",
  arrangement='snap',
  #domain =list(
  # x=c(0,1),
  #  y=c(0,1)
  #),
  orientation = "v",
  #  valueformat=".2f", #switch this on if doing per 6 trees otherwise it will rescale decimals
  node = list(pad = 15,
              thickness = 15,
              line = list(color = "black", width = 0.5),
              label = as.factor(node_col$name),
              color=node_col$col_top10_svd_fil
  ),
  link = list(source = allcrops_summarisedcounts$IDsource,
              target = allcrops_summarisedcounts$IDtarget,
              value = allcrops_summarisedcounts$average_count,
              #     value = test_merge_ave6trees$sum_6_tree),
              color=allcrops_summarisedcounts$rgba_top10_svd_fil
  )
) %>%
  layout(font = list(size = 10),
         xaxis = list(showgrid=F,zeroline=F),
         yaxis=list(showgrid=F,zeroline=F))

Diversity statistics

Alpha diversity analyses on all data (regardless of replication number)

Species diversity

Eddy needs to double check that TukeyHSD is the most appropriate posthoc test on the anova analyses

#count species per property
sppr <- specnumber(counts_wide)
#head(sppr)
#sppr
#zeros are in there now, just because of the merge above they are out of order
####################################################
#impact of crop, region and flowering time
###################################################

#crop
sppr_aov <-aov(sppr ~ Crop, data = meta_data)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7  13718  1959.7   175.8 <2e-16 ***
## Residuals   420   4682    11.1                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(sppr_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = sppr ~ Crop, data = meta_data)
## 
## $Crop
##                           diff          lwr        upr     p adj
## Avocado-Apple        4.8009185   3.03012342   6.571714 0.0000000
## Carrot-Apple        12.4536657  10.81629938  14.091032 0.0000000
## Kiwifruit-Apple      1.7479270  -0.03707788   3.532932 0.0598348
## Onion-Apple         14.8501493  12.94930758  16.750991 0.0000000
## Pak Choi-Apple      12.8481980  10.83142064  14.864975 0.0000000
## Pear-Apple           1.5451493  -0.48720733   3.577506 0.2872766
## Radish-Apple         6.2428765   2.93393591   9.551817 0.0000005
## Carrot-Avocado       7.6527473   6.00093630   9.304558 0.0000000
## Kiwifruit-Avocado   -3.0529915  -4.85125544  -1.254727 0.0000099
## Onion-Avocado       10.0492308   8.13593260  11.962529 0.0000000
## Pak Choi-Avocado     8.0472795   6.01875746  10.075802 0.0000000
## Pear-Avocado        -3.2557692  -5.29978100  -1.211757 0.0000469
## Radish-Avocado       1.4419580  -1.87415397   4.758070 0.8891829
## Kiwifruit-Carrot   -10.7057387 -12.37277404  -9.038703 0.0000000
## Onion-Carrot         2.3964835   0.60596311   4.187004 0.0013998
## Pak Choi-Carrot      0.3945323  -1.51862094   2.307686 0.9984796
## Pear-Carrot        -10.9085165 -12.83808575  -8.978947 0.0000000
## Radish-Carrot       -6.2107892  -9.45761109  -2.963967 0.0000003
## Onion-Kiwifruit     13.1022222  11.17576504  15.028679 0.0000000
## Pak Choi-Kiwifruit  11.1002710   9.05933267  13.141209 0.0000000
## Pear-Kiwifruit      -0.2027778  -2.25911226   1.853557 0.9999891
## Radish-Kiwifruit     4.4949495   1.17122775   7.818671 0.0011791
## Pak Choi-Onion      -2.0019512  -4.14493647   0.141034 0.0867866
## Pear-Onion         -13.3050000 -15.46265336 -11.147347 0.0000000
## Radish-Onion        -8.6072727 -11.99461428  -5.219931 0.0000000
## Pear-Pak Choi      -11.3030488 -13.56350449  -9.042593 0.0000000
## Radish-Pak Choi     -6.6053215 -10.05905496  -3.151588 0.0000003
## Radish-Pear          4.6977273   1.23487339   8.160581 0.0011191
#impact of region
sppr_aov <-aov(sppr ~ Region, data = meta_data)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Region       11  12574    1143   81.61 <2e-16 ***
## Residuals   416   5827      14                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(sppr_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = sppr ~ Region, data = meta_data)
## 
## $Region
##                                  diff         lwr          upr     p adj
## Bay of Plenty-Auckland     -8.9932432 -15.3077536  -2.67873293 0.0002408
## Canterbury-Auckland        -0.7282609  -6.9672304   5.51070866 0.9999998
## Hawkes Bay-Auckland       -11.3768657 -17.7082694  -5.04546193 0.0000005
## Marlborough-Auckland        2.6309524  -4.0797595   9.34166425 0.9802373
## Northland-Auckland         -8.3970588 -15.2329230  -1.56119469 0.0036773
## Otago-Auckland              4.3611111  -3.0308327  11.75305492 0.7338390
## Poverty Bay-Auckland      -12.5000000 -20.0327554  -4.96724461 0.0000055
## Southland-Auckland          0.9166667  -6.4752771   8.30861048 0.9999997
## Tasman-Auckland           -11.7954545 -18.1295600  -5.46134912 0.0000001
## Wairarapa-Auckland          0.4722222  -6.9197216   7.86416603 1.0000000
## Whangarei-Auckland         -7.7500000 -15.6902214   0.19022136 0.0631329
## Canterbury-Bay of Plenty    8.2649824   6.4926272  10.03733755 0.0000000
## Hawkes Bay-Bay of Plenty   -2.3836224  -4.4580321  -0.30921277 0.0098119
## Marlborough-Bay of Plenty  11.6241956   8.5827887  14.66560252 0.0000000
## Northland-Bay of Plenty     0.5961844  -2.7122201   3.90458895 0.9999854
## Otago-Bay of Plenty        13.3543544   9.0118510  17.69685771 0.0000000
## Poverty Bay-Bay of Plenty  -3.5067568  -8.0848465   1.07133301 0.3318207
## Southland-Bay of Plenty     9.9099099   5.5674066  14.25241327 0.0000000
## Tasman-Bay of Plenty       -2.8022113  -4.8848523  -0.71957027 0.0007566
## Wairarapa-Bay of Plenty     9.4654655   5.1229621  13.80796882 0.0000000
## Whangarei-Bay of Plenty     1.2432432  -3.9782142   6.46470065 0.9997633
## Hawkes Bay-Canterbury     -10.6486048 -12.4802368  -8.81697282 0.0000000
## Marlborough-Canterbury      3.3592133   0.4779191   6.24050744 0.0080201
## Northland-Canterbury       -7.6687980 -10.8306393  -4.50695659 0.0000000
## Otago-Canterbury            5.0893720   0.8574651   9.32127884 0.0050996
## Poverty Bay-Canterbury    -11.7717391 -16.2450607  -7.29841753 0.0000000
## Southland-Canterbury        1.6449275  -2.5869793   5.87683439 0.9815174
## Tasman-Canterbury         -11.0671937 -12.9081429  -9.22624448 0.0000000
## Wairarapa-Canterbury        1.2004831  -3.0314238   5.43238995 0.9987566
## Whangarei-Canterbury       -7.0217391 -12.1515849  -1.89189335 0.0005428
## Marlborough-Hawkes Bay     14.0078181  10.9314909  17.08414520 0.0000000
## Northland-Hawkes Bay        2.9798068  -0.3607280   6.32034174 0.1336418
## Otago-Hawkes Bay           15.7379768  11.3709448  20.10500875 0.0000000
## Poverty Bay-Hawkes Bay     -1.1231343  -5.7244970   3.47822837 0.9996990
## Southland-Hawkes Bay       12.2935323   7.9265004  16.66056431 0.0000000
## Tasman-Hawkes Bay          -0.4185889  -2.5519024   1.71472463 0.9999653
## Wairarapa-Hawkes Bay       11.8490879   7.4820559  16.21611986 0.0000000
## Whangarei-Hawkes Bay        3.6268657  -1.6150090   8.86874036 0.4967873
## Northland-Marlborough     -11.0280112 -15.0412585  -7.01476391 0.0000000
## Otago-Marlborough           1.7301587  -3.1706523   6.63096975 0.9914992
## Poverty Bay-Marlborough   -15.1309524 -20.2416783 -10.02022648 0.0000000
## Southland-Marlborough      -1.7142857  -6.6150967   3.18652531 0.9921280
## Tasman-Marlborough        -14.4264069 -17.5082906 -11.34452325 0.0000000
## Wairarapa-Marlborough      -2.1587302  -7.0595412   2.74208087 0.9531303
## Whangarei-Marlborough     -10.3809524 -16.0751802  -4.68672454 0.0000003
## Otago-Northland            12.7581699   7.6873383  17.82900155 0.0000000
## Poverty Bay-Northland      -4.1029412  -9.3769249   1.17104250 0.3079764
## Southland-Northland         9.3137255   4.2428939  14.38455711 0.0000002
## Tasman-Northland           -3.3983957  -6.7440484  -0.05274308 0.0426937
## Wairarapa-Northland         8.8692810   3.7984494  13.94011266 0.0000011
## Whangarei-Northland         0.6470588  -5.1941410   6.48825864 0.9999999
## Poverty Bay-Otago         -16.8611111 -22.8382926 -10.88392961 0.0000000
## Southland-Otago            -3.4444444  -9.2431622   2.35427336 0.7252112
## Tasman-Otago              -16.1565657 -20.5275137 -11.78561765 0.0000000
## Wairarapa-Otago            -3.8888889  -9.6876067   1.90982892 0.5474530
## Whangarei-Otago           -12.1111111 -18.5942747  -5.62794752 0.0000001
## Southland-Poverty Bay      13.4166667   7.4394852  19.39384817 0.0000000
## Tasman-Poverty Bay          0.7045455  -3.9005340   5.30962493 0.9999973
## Wairarapa-Poverty Bay      12.9722222   6.9950407  18.94940372 0.0000000
## Whangarei-Poverty Bay       4.7500000  -1.8932658  11.39326582 0.4427904
## Tasman-Southland          -12.7121212 -17.0830692  -8.34117321 0.0000000
## Wairarapa-Southland        -0.4444444  -6.2431622   5.35427336 1.0000000
## Whangarei-Southland        -8.6666667 -15.1498303  -2.18350307 0.0008554
## Wairarapa-Tasman           12.2676768   7.8967288  16.63862477 0.0000000
## Whangarei-Tasman            4.0454545  -1.1996831   9.29059215 0.3211661
## Whangarei-Wairarapa        -8.2222222 -14.7053858  -1.73905863 0.0021841
#impact of crop & region
sppr_aov <-aov(sppr ~ Crop+Region, data = meta_data)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7  13718  1959.7 179.357 <2e-16 ***
## Region       11    213    19.4   1.776  0.056 .  
## Residuals   409   4469    10.9                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(sppr_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = sppr ~ Crop + Region, data = meta_data)
## 
## $Crop
##                           diff          lwr         upr     p adj
## Avocado-Apple        4.8009185   3.04761595   6.5542210 0.0000000
## Carrot-Apple        12.4536657  10.83247386  14.0748576 0.0000000
## Kiwifruit-Apple      1.7479270  -0.01944498   3.5152990 0.0550215
## Onion-Apple         14.8501493  12.96808476  16.7322137 0.0000000
## Pak Choi-Apple      12.8481980  10.85134307  14.8450530 0.0000000
## Pear-Apple           1.5451493  -0.46713100   3.5574295 0.2748478
## Radish-Apple         6.2428765   2.96662278   9.5191303 0.0000004
## Carrot-Avocado       7.6527473   6.01725347   9.2882410 0.0000000
## Kiwifruit-Avocado   -3.0529915  -4.83349157  -1.2724913 0.0000077
## Onion-Avocado       10.0492308   8.15483283  11.9436287 0.0000000
## Pak Choi-Avocado     8.0472795   6.03879590  10.0557632 0.0000000
## Pear-Avocado        -3.2557692  -5.27958954  -1.2319489 0.0000375
## Radish-Avocado       1.4419580  -1.84139626   4.7253123 0.8838097
## Kiwifruit-Carrot   -10.7057387 -12.35630649  -9.0551709 0.0000000
## Onion-Carrot         2.3964835   0.62365050   4.1693165 0.0011906
## Pak Choi-Carrot      0.3945323  -1.49972215   2.2887867 0.9983774
## Pear-Carrot        -10.9085165 -12.81902479  -8.9980082 0.0000000
## Radish-Carrot       -6.2107892  -9.42553785  -2.9960406 0.0000002
## Onion-Kiwifruit     13.1022222  11.19479525  15.0096492 0.0000000
## Pak Choi-Kiwifruit  11.1002710   9.07949377  13.1210482 0.0000000
## Pear-Kiwifruit      -0.2027778  -2.23879907   1.8332435 0.9999884
## Radish-Kiwifruit     4.4949495   1.20406063   7.7858384 0.0009996
## Pak Choi-Onion      -2.0019512  -4.12376731   0.1198649 0.0805069
## Pear-Onion         -13.3050000 -15.44133930 -11.1686607 0.0000000
## Radish-Onion        -8.6072727 -11.96115293  -5.2533925 0.0000000
## Pear-Pak Choi      -11.3030488 -13.54117492  -9.0649226 0.0000000
## Radish-Pak Choi     -6.6053215 -10.02493778  -3.1857052 0.0000002
## Radish-Pear          4.6977273   1.26908067   8.1263739 0.0009478
## 
## $Region
##                                  diff         lwr       upr     p adj
## Bay of Plenty-Auckland     0.37424883  -5.2034604 5.9519581 1.0000000
## Canterbury-Auckland       -0.17062625  -5.6816091 5.3403566 1.0000000
## Hawkes Bay-Auckland        0.38450059  -5.2081309 5.9771321 1.0000000
## Marlborough-Auckland       0.62900116  -5.2986794 6.5566817 0.9999999
## Northland-Auckland        -0.34977927  -6.3880088 5.6884503 1.0000000
## Otago-Auckland             3.42426368  -3.1051601 9.9536874 0.8563726
## Poverty Bay-Auckland      -1.39972900  -8.0535339 5.2540759 0.9999300
## Southland-Auckland         0.91666667  -5.6127571 7.4460904 0.9999989
## Tasman-Auckland            0.02645322  -5.5685647 5.6214712 1.0000000
## Wairarapa-Auckland        -1.52972900  -8.0591527 4.9996947 0.9997975
## Whangarei-Auckland         0.29727955  -6.7164466 7.3110057 1.0000000
## Canterbury-Bay of Plenty  -0.54487507  -2.1104251 1.0206750 0.9924428
## Hawkes Bay-Bay of Plenty   0.01025177  -1.8221079 1.8426114 1.0000000
## Marlborough-Bay of Plenty  0.25475233  -2.4317717 2.9412763 1.0000000
## Northland-Bay of Plenty   -0.72402810  -3.6463954 2.1983392 0.9996520
## Otago-Bay of Plenty        3.05001485  -0.7857887 6.8858184 0.2754018
## Poverty Bay-Bay of Plenty -1.77397782  -5.8178787 2.2699230 0.9544234
## Southland-Bay of Plenty    0.54241784  -3.2933857 4.3782214 0.9999988
## Tasman-Bay of Plenty      -0.34779560  -2.1874262 1.4918350 0.9999762
## Wairarapa-Bay of Plenty   -1.90397782  -5.7397814 1.9318258 0.8965318
## Whangarei-Bay of Plenty   -0.07696928  -4.6891672 4.5352287 1.0000000
## Hawkes Bay-Canterbury      0.55512684  -1.0627834 2.1730370 0.9932973
## Marlborough-Canterbury     0.79962741  -1.7454664 3.3447212 0.9968698
## Northland-Canterbury      -0.17915303  -2.9720587 2.6137527 1.0000000
## Otago-Canterbury           3.59488992  -0.1432220 7.3330018 0.0723046
## Poverty Bay-Canterbury    -1.22910275  -5.1804602 2.7222547 0.9971349
## Southland-Canterbury       1.08729291  -2.6508190 4.8254048 0.9984336
## Tasman-Canterbury          0.19707947  -1.4290608 1.8232197 0.9999998
## Wairarapa-Canterbury      -1.35910275  -5.0972146 2.3790091 0.9891512
## Whangarei-Canterbury       0.46790580  -4.0633701 4.9991817 1.0000000
## Marlborough-Hawkes Bay     0.24450057  -2.4728691 2.9618702 1.0000000
## Northland-Hawkes Bay      -0.73427987  -3.6850285 2.2164687 0.9996370
## Otago-Hawkes Bay           3.03976308  -0.8177070 6.8972332 0.2886122
## Poverty Bay-Hawkes Bay    -1.78422959  -5.8486878 2.2802286 0.9542000
## Southland-Hawkes Bay       0.53216607  -3.3253040 4.3896362 0.9999991
## Tasman-Hawkes Bay         -0.35804737  -2.2424378 1.5263430 0.9999749
## Wairarapa-Hawkes Bay      -1.91422959  -5.7716997 1.9432405 0.8966996
## Whangarei-Hawkes Bay      -0.08722105  -4.7174539 4.5430118 1.0000000
## Northland-Marlborough     -0.97878044  -4.5237468 2.5661859 0.9990304
## Otago-Marlborough          2.79526252  -1.5337033 7.1242283 0.6068617
## Poverty Bay-Marlborough   -2.02873016  -6.5431172 2.4856568 0.9460531
## Southland-Marlborough      0.28766551  -4.0413003 4.6166313 1.0000000
## Tasman-Marlborough        -0.60254794  -3.3248257 2.1197298 0.9998841
## Wairarapa-Marlborough     -2.15873016  -6.4876959 2.1702356 0.8935406
## Whangarei-Marlborough     -0.33172161  -5.3615253 4.6980821 1.0000000
## Otago-Northland            3.77404295  -0.7051048 8.2531907 0.1971117
## Poverty Bay-Northland     -1.04994972  -5.7085450 3.6086455 0.9998614
## Southland-Northland        1.26644594  -3.2127018 5.7455937 0.9987919
## Tasman-Northland           0.37623250  -2.5790367 3.3315017 0.9999996
## Wairarapa-Northland       -1.17994972  -5.6590975 3.2991980 0.9993756
## Whangarei-Northland        0.64705882  -4.5125676 5.8066853 0.9999997
## Poverty Bay-Otago         -4.82399267 -10.1037339 0.4557486 0.1112829
## Southland-Otago           -2.50759701  -7.6296984 2.6145044 0.9050631
## Tasman-Otago              -3.39781045  -7.2587396 0.4631187 0.1473373
## Wairarapa-Otago           -4.95399267 -10.0760941 0.1681087 0.0686706
## Whangarei-Otago           -3.12698413  -8.8536676 2.5996993 0.8201319
## Southland-Poverty Bay      2.31639566  -2.9633456 7.5961369 0.9543845
## Tasman-Poverty Bay         1.42618222  -2.6415591 5.4939235 0.9919636
## Wairarapa-Poverty Bay     -0.13000000  -5.4097413 5.1497413 1.0000000
## Whangarei-Poverty Bay      1.69700855  -4.1710958 7.5651129 0.9985140
## Tasman-Southland          -0.89021344  -4.7511426 2.9707157 0.9998267
## Wairarapa-Southland       -2.44639566  -7.5684971 2.6757057 0.9189074
## Whangarei-Southland       -0.61938712  -6.3460706 5.1072963 0.9999999
## Wairarapa-Tasman          -1.55618222  -5.4171114 2.3047470 0.9755242
## Whangarei-Tasman           0.27082633  -4.3622887 4.9039414 1.0000000
## Whangarei-Wairarapa        1.82700855  -3.8996749 7.5536920 0.9964181
#region within a crop
#I did this for beta and then decided I prefer it because of the biased sampling across crops/regions
#radish has only one location
categories_crop <- meta_data %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
#filtered_sppr <- sppr[grepl('^Apple', names(sppr))]
filtered_sppr <- sppr[grepl(paste0('^',item), names(sppr))]
filtered_meta_data<-meta_data %>% filter(sample_ID %in% names(filtered_sppr))
sppr_aov <-aov(filtered_sppr ~ Region, data = filtered_meta_data)
print(summary(sppr_aov))
print(TukeyHSD(sppr_aov))
}
## [1] "Analysing Apple"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1   0.19  0.1913   0.133  0.717
## Residuals   65  93.75  1.4423               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data)
## 
## $Region
##                         diff        lwr       upr     p adj
## Tasman-Hawkes Bay -0.1114341 -0.7225623 0.4996941 0.7169195
## 
## [1] "Analysing Pear"
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Region       1   39.0   39.00   10.24 0.00278 **
## Residuals   38  144.8    3.81                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data)
## 
## $Region
##                        diff       lwr        upr     p adj
## Tasman-Hawkes Bay -2.154762 -3.518116 -0.7914079 0.0027771
## 
## [1] "Analysing Avocado"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       2    5.3   2.628   0.222  0.802
## Residuals   62  733.9  11.837               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data)
## 
## $Region
##                                  diff       lwr      upr     p adj
## Northland-Bay of Plenty -6.470588e-01 -3.021892 1.727774 0.7906585
## Whangarei-Bay of Plenty -8.881784e-16 -3.605591 3.605591 1.0000000
## Whangarei-Northland      6.470588e-01 -3.275956 4.570074 0.9172876
## 
## [1] "Analysing Kiwifruit"
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Region       3  50.81  16.937   5.257 0.00278 **
## Residuals   59 190.08   3.222                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data)
## 
## $Region
##                                 diff        lwr          upr     p adj
## Hawkes Bay-Bay of Plenty  -1.6805556 -3.4710155 0.1099044306 0.0733401
## Poverty Bay-Bay of Plenty -1.8750000 -3.7507672 0.0007672466 0.0501313
## Tasman-Bay of Plenty       0.5892857 -0.9312928 2.1098642091 0.7357862
## Poverty Bay-Hawkes Bay    -0.1944444 -2.5002737 2.1113848251 0.9960475
## Tasman-Hawkes Bay          2.2698413  0.2424033 4.2972792629 0.0223529
## Tasman-Poverty Bay         2.4642857  0.3611308 4.5674406341 0.0153827
## 
## [1] "Analysing Pakchoi"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3   45.1   15.03   0.907  0.447
## Residuals   37  613.3   16.58               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data)
## 
## $Region
##                             diff       lwr       upr     p adj
## Canterbury-Auckland   -0.5100000 -6.407272  5.387272 0.9954882
## Hawkes Bay-Auckland    3.2500000 -5.113934 11.613934 0.7242716
## Southland-Auckland     0.9166667 -5.664041  7.497375 0.9817868
## Hawkes Bay-Canterbury  3.7600000 -2.931147 10.451147 0.4410908
## Southland-Canterbury   1.4266667 -2.830301  5.683635 0.8041139
## Southland-Hawkes Bay  -2.3333333 -9.633973  4.967307 0.8253941
## 
## [1] "Analysing Onion"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3   34.2   11.41   0.462   0.71
## Residuals   46 1137.0   24.72               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data)
## 
## $Region
##                              diff       lwr      upr     p adj
## Marlborough-Canterbury  0.5142857 -3.965768 4.994340 0.9899256
## Otago-Canterbury        0.9333333 -5.910062 7.776729 0.9833602
## Wairarapa-Canterbury   -1.6444444 -7.232053 3.943165 0.8611163
## Otago-Marlborough       0.4190476 -6.175413 7.013509 0.9982465
## Wairarapa-Marlborough  -2.1587302 -7.438524 3.121064 0.6974441
## Wairarapa-Otago        -2.5777778 -9.969490 4.813934 0.7891899
## 
## [1] "Analysing Carrot"
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Region       1  164.3  164.30    10.8 0.00146 **
## Residuals   89 1354.4   15.22                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data)
## 
## $Region
##                      diff      lwr      upr     p adj
## Otago-Canterbury 6.554598 2.590838 10.51836 0.0014562
#impact of flowering time
sppr_aov <-aov(sppr ~ flowertime, data = meta_data)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## flowertime    1  12234   12234   845.3 <2e-16 ***
## Residuals   426   6166      14                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#TukeyHSD(sppr_aov)

#can plot that out
sppr_df <- sppr %>% 
  enframe() %>% 
  full_join(meta_data, by = c("name" = "sample_ID"))
#just picking a colour scheme for now will change depending on how we order the final plots
col_bound<-rcartocolor::carto_pal(n=9,name='Prism')
#col_bound<-c("#38381C", "#A04C00","#701C00","#285034","#E0C591","#E0DEDA","#CD951C","#7D99B5")
col_bound<-c("#6e7d3c", "#007b83","#b78080","#b5a53c","#E0C591","#516295","#CD951C","#6baed6")
#Trying to order by flowering time
#this is based of brads chart but note
#apple and pear flower at the same time so Radish, Carrot and Onion so they are ordered alphabetically
#so maybe its better to just say grouped by spring and summer flowering?
#sppr_df$Crop <- factor(sppr_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
#reordering based on brads order
sppr_df$Crop <- factor(sppr_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

plot_sppr <- ggplot(sppr_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  scale_fill_manual(values=col_bound)+
  theme_bw()+
  labs(x = "Crop",
       y = "Number of species per sample",
       title = "Species richness")
plot_sppr

#other significant factor was region
plot_sppr <- ggplot(sppr_df, aes(x = Region, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~Crop, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Number of species per sample",
       title = "Species richness")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

#other significant factor was flowertime
plot_sppr <- ggplot(sppr_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
 # facet_wrap(~flowertime, scales="free")+
  facet_wrap(~flowertime, scales = "free_x")+
#  facet_grid(.~flowertime,drop = TRUE) +
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Number of species per sample",
       title = "Species richness")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

Simpson

#########
#simpson#
#########

#using simpson which is less sensitive to rare species which should be more robust to the sampling biases in the data
simpsondiv<-diversity(counts_wide,index='simpson')
#head(simpsondiv)
####################################################
#impact of crop, region and flowering time
###################################################

#crop
simpsondiv_aov <-aov(simpsondiv ~ Crop, data = meta_data)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7  7.656  1.0937   15.66 <2e-16 ***
## Residuals   420 29.340  0.0699                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(simpsondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = simpsondiv ~ Crop, data = meta_data)
## 
## $Crop
##                            diff          lwr          upr     p adj
## Avocado-Apple      -0.030217764 -0.170392206  0.109956677 0.9979801
## Carrot-Apple        0.236536499  0.106924146  0.366148851 0.0000013
## Kiwifruit-Apple    -0.122952614 -0.264251894  0.018346665 0.1411042
## Onion-Apple         0.228728601  0.078259795  0.379197407 0.0001310
## Pak Choi-Apple      0.044205289 -0.115440878  0.203851456 0.9904551
## Pear-Apple         -0.047350070 -0.208229471  0.113529330 0.9862868
## Radish-Apple       -0.015809395 -0.277741965  0.246123176 0.9999996
## Carrot-Avocado      0.266754263  0.135998490  0.397510036 0.0000000
## Kiwifruit-Avocado  -0.092734850 -0.235083705  0.049614005 0.4935943
## Onion-Avocado       0.258946365  0.107491514  0.410401216 0.0000082
## Pak Choi-Avocado    0.074423053 -0.086152813  0.234998919 0.8515717
## Pear-Avocado       -0.017132306 -0.178934320  0.144669708 0.9999823
## Radish-Avocado      0.014408370 -0.248091881  0.276908621 0.9999998
## Kiwifruit-Carrot   -0.359489113 -0.491450034 -0.227528192 0.0000000
## Onion-Carrot       -0.007807898 -0.149543778  0.133927982 0.9999998
## Pak Choi-Carrot    -0.192331210 -0.343774588 -0.040887832 0.0031638
## Pear-Carrot        -0.283886569 -0.436629424 -0.131143714 0.0000008
## Radish-Carrot      -0.252345893 -0.509361204  0.004669417 0.0585191
## Onion-Kiwifruit     0.351681215  0.199184709  0.504177721 0.0000000
## Pak Choi-Kiwifruit  0.167157903  0.005599179  0.328716627 0.0366488
## Pear-Kiwifruit      0.075602544 -0.087174924  0.238380012 0.8501762
## Radish-Kiwifruit    0.107143220 -0.155959411  0.370245850 0.9193118
## Pak Choi-Onion     -0.184523312 -0.354159972 -0.014886652 0.0221842
## Pear-Onion         -0.276078671 -0.446876444 -0.105280898 0.0000333
## Radish-Onion       -0.244537995 -0.512676708  0.023600718 0.1033138
## Pear-Pak Choi      -0.091555359 -0.270490868  0.087380150 0.7746063
## Radish-Pak Choi    -0.060014683 -0.333408916  0.213379549 0.9977328
## Radish-Pear         0.031540676 -0.242575521  0.305656872 0.9999688
#impact of region
simpsondiv_aov <-aov(simpsondiv ~ Region, data = meta_data)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## Region       11  6.694  0.6085   8.354 2.58e-13 ***
## Residuals   416 30.302  0.0728                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(simpsondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = simpsondiv ~ Region, data = meta_data)
## 
## $Region
##                                   diff         lwr         upr     p adj
## Bay of Plenty-Auckland     0.078332291 -0.37704326  0.53370784 0.9999910
## Canterbury-Auckland        0.348921954 -0.10100591  0.79884982 0.3127820
## Hawkes Bay-Auckland        0.136202889 -0.32039094  0.59279672 0.9980333
## Marlborough-Auckland       0.365304571 -0.11864334  0.84925248 0.3548515
## Northland-Auckland         0.178888485 -0.31408488  0.67186185 0.9893351
## Otago-Auckland             0.436486777 -0.09658868  0.96956223 0.2342766
## Poverty Bay-Auckland      -0.024253364 -0.56748355  0.51897682 1.0000000
## Southland-Auckland         0.139927882 -0.39314757  0.67300334 0.9993973
## Tasman-Auckland            0.128015112 -0.32877355  0.58480378 0.9988884
## Wairarapa-Auckland         0.363981971 -0.16909348  0.89705743 0.5184228
## Whangarei-Auckland         0.091202301 -0.48141259  0.66381720 0.9999959
## Canterbury-Bay of Plenty   0.270589664  0.14277497  0.39840436 0.0000000
## Hawkes Bay-Bay of Plenty   0.057870598 -0.09172698  0.20746817 0.9821965
## Marlborough-Bay of Plenty  0.286972280  0.06763899  0.50630557 0.0012694
## Northland-Bay of Plenty    0.100556194 -0.13803183  0.33914422 0.9658743
## Otago-Bay of Plenty        0.358154486  0.04499167  0.67131730 0.0104599
## Poverty Bay-Bay of Plenty -0.102585654 -0.43273796  0.22756665 0.9971664
## Southland-Bay of Plenty    0.061595591 -0.25156723  0.37475841 0.9999645
## Tasman-Bay of Plenty       0.049682822 -0.10050837  0.19987401 0.9951002
## Wairarapa-Bay of Plenty    0.285649681 -0.02751314  0.59881250 0.1127835
## Whangarei-Bay of Plenty    0.012870010 -0.36367922  0.38941925 1.0000000
## Hawkes Bay-Canterbury     -0.212719065 -0.34480855 -0.08062958 0.0000126
## Marlborough-Canterbury     0.016382616 -0.19140403  0.22416926 1.0000000
## Northland-Canterbury      -0.170033469 -0.39805198  0.05798504 0.3743062
## Otago-Canterbury           0.087564823 -0.21762225  0.39275189 0.9986196
## Poverty Bay-Canterbury    -0.373175318 -0.69577219 -0.05057845 0.0089474
## Southland-Canterbury      -0.208994073 -0.51418114  0.09619300 0.5136352
## Tasman-Canterbury         -0.220906842 -0.35366825 -0.08814544 0.0000051
## Wairarapa-Canterbury       0.015060017 -0.29012705  0.32024709 1.0000000
## Whangarei-Canterbury      -0.257719653 -0.62766225  0.11222294 0.4855772
## Marlborough-Hawkes Bay     0.229101682  0.00725009  0.45095327 0.0358971
## Northland-Hawkes Bay       0.042685596 -0.19821953  0.28359072 0.9999877
## Otago-Hawkes Bay           0.300283888 -0.01464783  0.61521561 0.0780311
## Poverty Bay-Hawkes Bay    -0.160456253 -0.49228690  0.17137440 0.9122860
## Southland-Hawkes Bay       0.003724993 -0.31120672  0.31865671 1.0000000
## Tasman-Hawkes Bay         -0.008187777 -0.16203325  0.14565769 1.0000000
## Wairarapa-Hawkes Bay       0.227779082 -0.08715263  0.54271080 0.4239433
## Whangarei-Hawkes Bay      -0.045000588 -0.42302223  0.33302105 0.9999998
## Northland-Marlborough     -0.186416086 -0.47583436  0.10300219 0.6108134
## Otago-Marlborough          0.071182206 -0.28224338  0.42460779 0.9999549
## Poverty Bay-Marlborough   -0.389557934 -0.75812169 -0.02099418 0.0277943
## Southland-Marlborough     -0.225376689 -0.57880227  0.12804890 0.6259751
## Tasman-Marlborough        -0.237289458 -0.45954176 -0.01503715 0.0247692
## Wairarapa-Marlborough     -0.001322599 -0.35474818  0.35210299 1.0000000
## Whangarei-Marlborough     -0.274102270 -0.68474569  0.13654115 0.5549974
## Otago-Northland            0.257598292 -0.10808845  0.62328504 0.4673311
## Poverty Bay-Northland     -0.203141849 -0.58347905  0.17719536 0.8408060
## Southland-Northland       -0.038960603 -0.40464735  0.32672614 0.9999999
## Tasman-Northland          -0.050873373 -0.29214757  0.19040082 0.9999285
## Wairarapa-Northland        0.185093486 -0.18059326  0.55078023 0.8835301
## Whangarei-Northland       -0.087686184 -0.50892860  0.33355623 0.9999371
## Poverty Bay-Otago         -0.460740141 -0.89178897 -0.02969131 0.0244425
## Southland-Otago           -0.296558895 -0.71473769  0.12161990 0.4562860
## Tasman-Otago              -0.308471665 -0.62368579  0.00674246 0.0615997
## Wairarapa-Otago           -0.072504806 -0.49068360  0.34567399 0.9999902
## Whangarei-Otago           -0.345284476 -0.81282258  0.12225363 0.3899233
## Southland-Poverty Bay      0.164181245 -0.26686759  0.59523008 0.9842455
## Tasman-Poverty Bay         0.152268476 -0.17983021  0.48436716 0.9381749
## Wairarapa-Poverty Bay      0.388235335 -0.04281350  0.81928417 0.1243299
## Whangarei-Poverty Bay      0.115455665 -0.36362833  0.59453966 0.9997340
## Tasman-Southland          -0.011912769 -0.32712689  0.30330136 1.0000000
## Wairarapa-Southland        0.224054090 -0.19412470  0.64223288 0.8379997
## Whangarei-Southland       -0.048725581 -0.51626368  0.41881252 1.0000000
## Wairarapa-Tasman           0.235966859 -0.07924727  0.55118098 0.3680999
## Whangarei-Tasman          -0.036812811 -0.41506976  0.34144414 1.0000000
## Whangarei-Wairarapa       -0.272779670 -0.74031777  0.19475843 0.7476537
#impact of crop and region
simpsondiv_aov <-aov(simpsondiv ~ Crop+Region, data = meta_data)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7  7.656  1.0937   15.49 <2e-16 ***
## Region       11  0.459  0.0417    0.59  0.837    
## Residuals   409 28.882  0.0706                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(simpsondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = simpsondiv ~ Crop + Region, data = meta_data)
## 
## $Crop
##                            diff          lwr          upr     p adj
## Avocado-Apple      -0.030217764 -0.171169414  0.110733885 0.9980476
## Carrot-Apple        0.236536499  0.106205500  0.366867497 0.0000016
## Kiwifruit-Apple    -0.122952614 -0.265035339  0.019130110 0.1459932
## Onion-Apple         0.228728601  0.077425508  0.380031693 0.0001477
## Pak Choi-Apple      0.044205289 -0.116326049  0.204736626 0.9907559
## Pear-Apple         -0.047350070 -0.209121479  0.114421339 0.9867105
## Radish-Apple       -0.015809395 -0.279194271  0.247575482 0.9999996
## Carrot-Avocado      0.266754263  0.135273505  0.398235022 0.0000000
## Kiwifruit-Avocado  -0.092734850 -0.235872970  0.050403270 0.5009102
## Onion-Avocado       0.258946365  0.106651761  0.411240969 0.0000096
## Pak Choi-Avocado    0.074423053 -0.087043139  0.235889245 0.8550822
## Pear-Avocado       -0.017132306 -0.179831444  0.145566832 0.9999830
## Radish-Avocado      0.014408370 -0.249547334  0.278364074 0.9999998
## Kiwifruit-Carrot   -0.359489113 -0.492181702 -0.226796525 0.0000000
## Onion-Carrot       -0.007807898 -0.150329644  0.134713848 0.9999998
## Pak Choi-Carrot    -0.192331210 -0.344614278 -0.040048142 0.0034355
## Pear-Carrot        -0.283886569 -0.437476319 -0.130296819 0.0000009
## Radish-Carrot      -0.252345893 -0.510786245  0.006094459 0.0612780
## Onion-Kiwifruit     0.351681215  0.198339180  0.505023250 0.0000000
## Pak Choi-Kiwifruit  0.167157903  0.004703404  0.329612402 0.0386116
## Pear-Kiwifruit      0.075602544 -0.088077457  0.239282545 0.8537127
## Radish-Kiwifruit    0.107143220 -0.157418204  0.371704644 0.9214315
## Pak Choi-Onion     -0.184523312 -0.355100536 -0.013946088 0.0235222
## Pear-Onion         -0.276078671 -0.447823446 -0.104333896 0.0000382
## Radish-Onion       -0.244537995 -0.514163424  0.025087434 0.1073594
## Pear-Pak Choi      -0.091555359 -0.271482990  0.088372272 0.7794090
## Radish-Pak Choi    -0.060014683 -0.334924772  0.214895405 0.9978084
## Radish-Pear         0.031540676 -0.244095380  0.307176731 0.9999699
## 
## $Region
##                                   diff        lwr       upr     p adj
## Bay of Plenty-Auckland     0.192856901 -0.2555468 0.6412606 0.9604167
## Canterbury-Auckland        0.212396567 -0.2306428 0.6554360 0.9169222
## Hawkes Bay-Auckland        0.203425469 -0.2461778 0.6530288 0.9434707
## Marlborough-Auckland       0.180781259 -0.2957573 0.6573199 0.9847207
## Northland-Auckland         0.253311538 -0.2321143 0.7387374 0.8604307
## Otago-Auckland             0.248493288 -0.2764207 0.7734073 0.9234532
## Poverty Bay-Auckland       0.142904540 -0.3920087 0.6778178 0.9992874
## Southland-Auckland         0.139927882 -0.3849861 0.6648419 0.9993018
## Tasman-Auckland            0.218389167 -0.2314060 0.6681843 0.9098978
## Wairarapa-Auckland         0.179458659 -0.3454553 0.7043727 0.9934981
## Whangarei-Auckland         0.165625354 -0.3982227 0.7294734 0.9982869
## Canterbury-Bay of Plenty   0.019539667 -0.1063182 0.1453975 0.9999969
## Hawkes Bay-Bay of Plenty   0.010568568 -0.1367386 0.1578758 1.0000000
## Marlborough-Bay of Plenty -0.012075642 -0.2280509 0.2038996 1.0000000
## Northland-Bay of Plenty    0.060454638 -0.1744806 0.2953898 0.9995000
## Otago-Bay of Plenty        0.055636388 -0.2527319 0.3640046 0.9999852
## Poverty Bay-Bay of Plenty -0.049952361 -0.3750500 0.2751453 0.9999972
## Southland-Bay of Plenty   -0.052929019 -0.3612973 0.2554392 0.9999911
## Tasman-Bay of Plenty       0.025532267 -0.1223595 0.1734240 0.9999906
## Wairarapa-Bay of Plenty   -0.013398241 -0.3217665 0.2949700 1.0000000
## Whangarei-Bay of Plenty   -0.027231547 -0.3980158 0.3435527 1.0000000
## Hawkes Bay-Canterbury     -0.008971098 -0.1390383 0.1210961 1.0000000
## Marlborough-Canterbury    -0.031615308 -0.2362207 0.1729901 0.9999970
## Northland-Canterbury       0.040914971 -0.1836125 0.2654425 0.9999836
## Otago-Canterbury           0.036096721 -0.2644179 0.3366113 0.9999998
## Poverty Bay-Canterbury    -0.069492028 -0.3871499 0.2481658 0.9998967
## Southland-Canterbury      -0.072468686 -0.3729833 0.2280459 0.9997318
## Tasman-Canterbury          0.005992600 -0.1247362 0.1367214 1.0000000
## Wairarapa-Canterbury      -0.032937908 -0.3334525 0.2675767 0.9999999
## Whangarei-Canterbury      -0.046771213 -0.4110499 0.3175075 0.9999996
## Marlborough-Hawkes Bay    -0.022644210 -0.2410992 0.1958108 1.0000000
## Northland-Hawkes Bay       0.049886069 -0.1873308 0.2871029 0.9999303
## Otago-Hawkes Bay           0.045067819 -0.2650423 0.3551779 0.9999984
## Poverty Bay-Hawkes Bay    -0.060520929 -0.3872712 0.2662294 0.9999807
## Southland-Hawkes Bay      -0.063497587 -0.3736077 0.2466125 0.9999466
## Tasman-Hawkes Bay          0.014963698 -0.1365264 0.1664538 1.0000000
## Wairarapa-Hawkes Bay      -0.023966809 -0.3340769 0.2861433 1.0000000
## Whangarei-Hawkes Bay      -0.037800115 -0.4100342 0.3344340 1.0000000
## Northland-Marlborough      0.072530279 -0.2124570 0.3575175 0.9995498
## Otago-Marlborough          0.067712029 -0.2803026 0.4157266 0.9999681
## Poverty Bay-Marlborough   -0.037876719 -0.4007977 0.3250443 1.0000000
## Southland-Marlborough     -0.040853377 -0.3888680 0.3071612 0.9999998
## Tasman-Marlborough         0.037607909 -0.1812417 0.2564575 0.9999910
## Wairarapa-Marlborough     -0.001322599 -0.3493372 0.3466920 1.0000000
## Whangarei-Marlborough     -0.015155905 -0.4195123 0.3892005 1.0000000
## Otago-Northland           -0.004818250 -0.3649063 0.3552698 1.0000000
## Poverty Bay-Northland     -0.110406999 -0.4849212 0.2641072 0.9982300
## Southland-Northland       -0.113383657 -0.4734717 0.2467044 0.9968083
## Tasman-Northland          -0.034922371 -0.2725026 0.2026579 0.9999982
## Wairarapa-Northland       -0.073852879 -0.4339409 0.2862352 0.9999457
## Whangarei-Northland       -0.087686184 -0.5024793 0.3271070 0.9999265
## Poverty Bay-Otago         -0.105588749 -0.5300382 0.3188607 0.9996381
## Southland-Otago           -0.108565407 -0.5203418 0.3032110 0.9993707
## Tasman-Otago              -0.030104121 -0.3404923 0.2802840 1.0000000
## Wairarapa-Otago           -0.069034629 -0.4808110 0.3427418 0.9999931
## Whangarei-Otago           -0.082867934 -0.5432480 0.3775121 0.9999856
## Southland-Poverty Bay     -0.002976658 -0.4274261 0.4214728 1.0000000
## Tasman-Poverty Bay         0.075484628 -0.2515296 0.4024988 0.9998248
## Wairarapa-Poverty Bay      0.036554120 -0.3878953 0.4610035 1.0000000
## Whangarei-Poverty Bay      0.022720815 -0.4490283 0.4944700 1.0000000
## Tasman-Southland           0.078461286 -0.2319269 0.3888494 0.9995780
## Wairarapa-Southland        0.039530778 -0.3722456 0.4513072 1.0000000
## Whangarei-Southland        0.025697472 -0.4346826 0.4860775 1.0000000
## Wairarapa-Tasman          -0.038930508 -0.3493187 0.2714576 0.9999997
## Whangarei-Tasman          -0.052763813 -0.4252296 0.3197020 0.9999988
## Whangarei-Wairarapa       -0.013833305 -0.4742133 0.4465467 1.0000000
#region within a crop
#I did this for beta and then decided I prefer it because of the biased sampling across crops/regions
#radish has only one location
categories_crop <- meta_data %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
#filtered_sppr <- sppr[grepl('^Apple', names(sppr))]
filtered_simpsondiv <- simpsondiv[grepl(paste0('^',item), names(simpsondiv))]
filtered_meta_data<-meta_data %>% filter(sample_ID %in% names(filtered_simpsondiv))
simpsondiv_aov <-aov(filtered_simpsondiv ~ Region, data = filtered_meta_data)
print(summary(simpsondiv_aov))
print(TukeyHSD(simpsondiv_aov))
}
## [1] "Analysing Apple"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1  0.126  0.1259   0.673  0.415
## Residuals   65 12.154  0.1870               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                        diff        lwr       upr     p adj
## Tasman-Hawkes Bay -0.090412 -0.3104534 0.1296294 0.4148754
## 
## [1] "Analysing Pear"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1  0.232  0.2320   2.142  0.152
## Residuals   38  4.116  0.1083               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                        diff        lwr       upr     p adj
## Tasman-Hawkes Bay 0.1662014 -0.0636858 0.3960886 0.1515336
## 
## [1] "Analysing Avocado"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       2 0.0605 0.03024   0.696  0.502
## Residuals   62 2.6924 0.04343               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                                diff         lwr       upr     p adj
## Northland-Bay of Plenty  0.06500406 -0.07883848 0.2088466 0.5267952
## Whangarei-Bay of Plenty -0.02268213 -0.24107114 0.1957069 0.9663120
## Whangarei-Northland     -0.08768618 -0.32530142 0.1499290 0.6510261
## 
## [1] "Analysing Kiwifruit"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3  0.117 0.03883   0.365  0.778
## Residuals   59  6.275 0.10636               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                                  diff        lwr       upr     p adj
## Hawkes Bay-Bay of Plenty  -0.03729886 -0.3626249 0.2880272 0.9902293
## Poverty Bay-Bay of Plenty -0.05592347 -0.3967499 0.2849029 0.9724116
## Tasman-Bay of Plenty       0.07567469 -0.2006140 0.3519634 0.8870733
## Poverty Bay-Hawkes Bay    -0.01862461 -0.4375931 0.4003439 0.9994128
## Tasman-Hawkes Bay          0.11297355 -0.2554114 0.4813585 0.8491254
## Tasman-Poverty Bay         0.13159816 -0.2505445 0.5137408 0.7993519
## 
## [1] "Analysing Pakchoi"
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Region       3 0.2918 0.09728   2.726 0.0579 .
## Residuals   37 1.3204 0.03569                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                              diff         lwr        upr     p adj
## Canterbury-Auckland    0.24280198 -0.03083279 0.51643674 0.0973993
## Hawkes Bay-Auckland    0.33413560 -0.05395281 0.72222400 0.1127644
## Southland-Auckland     0.13992788 -0.16541844 0.44527421 0.6105373
## Hawkes Bay-Canterbury  0.09133362 -0.21913710 0.40180435 0.8579440
## Southland-Canterbury  -0.10287409 -0.30039837 0.09465018 0.5068087
## Southland-Hawkes Bay  -0.19420772 -0.53295905 0.14454362 0.4235200
## 
## [1] "Analysing Onion"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3 0.0411 0.01370   0.847  0.475
## Residuals   46 0.7443 0.01618               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                                diff         lwr        upr     p adj
## Marlborough-Canterbury -0.050901985 -0.16552540 0.06372143 0.6400772
## Otago-Canterbury        0.023108232 -0.15198192 0.19819838 0.9848712
## Wairarapa-Canterbury   -0.052224584 -0.19518509 0.09073593 0.7649682
## Otago-Marlborough       0.074010217 -0.09471087 0.24273131 0.6490195
## Wairarapa-Marlborough  -0.001322599 -0.13640758 0.13376239 0.9999935
## Wairarapa-Otago        -0.075332816 -0.26445179 0.11378616 0.7141963
## 
## [1] "Analysing Carrot"
##             Df Sum Sq  Mean Sq F value Pr(>F)
## Region       1 0.0059 0.005854   0.668  0.416
## Residuals   89 0.7798 0.008762               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                        diff         lwr       upr    p adj
## Otago-Canterbury 0.03912555 -0.05598479 0.1342359 0.415892
#impact of flowering time
simpsondiv_aov <-aov(simpsondiv ~ flowertime, data = meta_data)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## flowertime    1   5.54   5.540   75.03 <2e-16 ***
## Residuals   426  31.46   0.074                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#TukeyHSD(simpsondiv_aov)

#can plot that out
simpsondiv_df <- simpsondiv %>% 
  enframe() %>% 
  full_join(meta_data, by = c("name" = "sample_ID"))

#simpsondiv_df$Crop <- factor(simpsondiv_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
#brads order
simpsondiv_df$Crop <- factor(simpsondiv_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

#just crop
plot_sppr <- ggplot(simpsondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  scale_fill_manual(values=col_bound)+
  theme_bw()+
  labs(x = "Crop",
       y = "Simpson",
       title = "Species richness: simpson")
plot_sppr

#other significant factor was flowertime
plot_sppr <- ggplot(simpsondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~flowertime, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Simpson",
       title = "Species richness: simpson")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

Shannon

I actually dont like Simpson, when say apple has a bunch of 1’s its getting a very high score as its perfectly even. So trying shannon as an alternative

#########
#shannon#
#########


#having a look at shannon
shannondiv<-diversity(counts_wide)
#head(shannondiv)
#crop
shannondiv_aov <-aov(shannondiv ~ Crop, data = meta_data)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7 193.02   27.57   131.3 <2e-16 ***
## Residuals   420  88.23    0.21                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tukey_test<-TukeyHSD(shannondiv_aov)
tukey_test<-tukey_test$Crop
write.csv(tukey_test,'Alpha_shannon_diversity_allreplicates_countsvscrops.csv',quote = F,row.names = T)


#impact of region
shannondiv_aov <-aov(shannondiv ~ Region, data = meta_data)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Region       11  173.9  15.809   61.26 <2e-16 ***
## Residuals   416  107.4   0.258                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(shannondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = shannondiv ~ Region, data = meta_data)
## 
## $Region
##                                   diff         lwr         upr     p adj
## Bay of Plenty-Auckland    -0.020392176 -0.87752036  0.83673601 1.0000000
## Canterbury-Auckland        0.891560471  0.04468615  1.73843479 0.0290673
## Hawkes Bay-Auckland       -0.547842393 -1.40726368  0.31157890 0.6265335
## Marlborough-Auckland       1.059372258  0.14846389  1.97028063 0.0083046
## Northland-Auckland         0.223678330 -0.70421814  1.15157480 0.9997332
## Otago-Auckland             1.303487394  0.30010897  2.30686582 0.0014415
## Poverty Bay-Auckland      -0.734130209 -1.75662232  0.28836190 0.4359747
## Southland-Auckland         0.225384930 -0.77799349  1.22876335 0.9998662
## Tasman-Auckland           -0.458749261 -1.31853728  0.40103875 0.8417196
## Wairarapa-Auckland         0.930325210 -0.07305321  1.93370363 0.0989330
## Whangarei-Auckland         0.075917532 -1.00188379  1.15371885 1.0000000
## Canterbury-Bay of Plenty   0.911952647  0.67137412  1.15253117 0.0000000
## Hawkes Bay-Bay of Plenty  -0.527450217 -0.80902945 -0.24587098 0.0000001
## Marlborough-Bay of Plenty  1.079764434  0.66692552  1.49260335 0.0000000
## Northland-Bay of Plenty    0.244070506 -0.20501052  0.69315153 0.8247519
## Otago-Bay of Plenty        1.323879570  0.73443053  1.91332861 0.0000000
## Poverty Bay-Bay of Plenty -0.713738033 -1.33516544 -0.09231063 0.0098721
## Southland-Bay of Plenty    0.245777105 -0.34367193  0.83522614 0.9684699
## Tasman-Bay of Plenty      -0.438357086 -0.72105364 -0.15566053 0.0000336
## Wairarapa-Bay of Plenty    0.950717385  0.36126835  1.54016642 0.0000121
## Whangarei-Bay of Plenty    0.096309708 -0.61244807  0.80506749 0.9999992
## Hawkes Bay-Canterbury     -1.439402864 -1.68802759 -1.19077814 0.0000000
## Marlborough-Canterbury     0.167811787 -0.22329351  0.55891709 0.9611322
## Northland-Canterbury      -0.667882141 -1.09706876 -0.23869552 0.0000308
## Otago-Canterbury           0.411926923 -0.16250981  0.98636365 0.4379991
## Poverty Bay-Canterbury    -1.625690680 -2.23289691 -1.01848444 0.0000000
## Southland-Canterbury      -0.666175541 -1.24061227 -0.09173881 0.0086395
## Tasman-Canterbury         -1.350309732 -1.60019917 -1.10042029 0.0000000
## Wairarapa-Canterbury       0.038764739 -0.53567199  0.61320147 1.0000000
## Whangarei-Canterbury      -0.815642939 -1.51196541 -0.11932047 0.0075035
## Marlborough-Hawkes Bay     1.607214651  1.18963568  2.02479362 0.0000000
## Northland-Hawkes Bay       0.771520723  0.31807834  1.22496311 0.0000026
## Otago-Hawkes Bay           1.851329787  1.25855125  2.44410832 0.0000000
## Poverty Bay-Hawkes Bay    -0.186287816 -0.81087428  0.43829865 0.9980360
## Southland-Hawkes Bay       0.773227323  0.18044879  1.36600586 0.0013405
## Tasman-Hawkes Bay          0.089093132 -0.20048168  0.37866794 0.9974046
## Wairarapa-Hawkes Bay       1.478167603  0.88538907  2.07094614 0.0000000
## Whangarei-Hawkes Bay       0.623759925 -0.08776929  1.33528914 0.1514640
## Northland-Marlborough     -0.835693928 -1.38044993 -0.29093793 0.0000439
## Otago-Marlborough          0.244115136 -0.42111828  0.90934855 0.9883161
## Poverty Bay-Marlborough   -1.793502467 -2.48722961 -1.09977532 0.0000000
## Southland-Marlborough     -0.833987328 -1.49922074 -0.16875391 0.0026477
## Tasman-Marlborough        -1.518121519 -1.93645473 -1.09978831 0.0000000
## Wairarapa-Marlborough     -0.129047048 -0.79428046  0.53618637 0.9999691
## Whangarei-Marlborough     -0.983454726 -1.75638611 -0.21052334 0.0020673
## Otago-Northland            1.079809064  0.39149715  1.76812098 0.0000250
## Poverty Bay-Northland     -0.957808539 -1.67369621 -0.24192087 0.0008419
## Southland-Northland        0.001706599 -0.68660532  0.69001852 1.0000000
## Tasman-Northland          -0.682427592 -1.13656466 -0.22829053 0.0000723
## Wairarapa-Northland        0.706646880  0.01833496  1.39495880 0.0382084
## Whangarei-Northland       -0.147760798 -0.94064208  0.64512048 0.9999795
## Poverty Bay-Otago         -2.037617603 -2.84895696 -1.22627825 0.0000000
## Southland-Otago           -1.078102464 -1.86521726 -0.29098767 0.0005359
## Tasman-Otago              -1.762236655 -2.35554675 -1.16892656 0.0000000
## Wairarapa-Otago           -0.373162184 -1.16027698  0.41395261 0.9227881
## Whangarei-Otago           -1.227569862 -2.10759095 -0.34754877 0.0003714
## Southland-Poverty Bay      0.959515138  0.14817578  1.77085450 0.0065368
## Tasman-Poverty Bay         0.275380947 -0.34971003  0.90047192 0.9530844
## Wairarapa-Poverty Bay      1.664455418  0.85311606  2.47579478 0.0000000
## Whangarei-Poverty Bay      0.810047741 -0.09170554  1.71180102 0.1268297
## Tasman-Southland          -0.684134191 -1.27744429 -0.09082409 0.0093530
## Wairarapa-Southland        0.704940280 -0.08217451  1.49205507 0.1297241
## Whangarei-Southland       -0.149467397 -1.02948849  0.73055369 0.9999921
## Wairarapa-Tasman           1.389074471  0.79576437  1.98238457 0.0000000
## Whangarei-Tasman           0.534666794 -0.17730532  1.24663891 0.3630418
## Whangarei-Wairarapa       -0.854407678 -1.73442877  0.02561341 0.0663055
#impact of crop and region
shannondiv_aov <-aov(shannondiv ~ Crop+Region, data = meta_data)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7 193.02  27.575 135.843 <2e-16 ***
## Region       11   5.21   0.474   2.334 0.0086 ** 
## Residuals   409  83.02   0.203                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(shannondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = shannondiv ~ Crop + Region, data = meta_data)
## 
## $Crop
##                           diff         lwr        upr     p adj
## Avocado-Apple       0.89220666  0.65322760  1.1311857 0.0000000
## Carrot-Apple        1.78084911  1.55987703  2.0018212 0.0000000
## Kiwifruit-Apple     0.44980689  0.20891013  0.6907037 0.0000007
## Onion-Apple         1.83424985  1.57772025  2.0907795 0.0000000
## Pak Choi-Apple      1.22507545  0.95289965  1.4972512 0.0000000
## Pear-Apple          0.40659859  0.13232029  0.6808769 0.0002201
## Radish-Apple        0.94181391  0.49525320  1.3883746 0.0000000
## Carrot-Avocado      0.88864246  0.66572099  1.1115639 0.0000000
## Kiwifruit-Avocado  -0.44239977 -0.68508591 -0.1997136 0.0000014
## Onion-Avocado       0.94204320  0.68383252  1.2002539 0.0000000
## Pak Choi-Avocado    0.33286879  0.05910797  0.6066296 0.0058453
## Pear-Avocado       -0.48560806 -0.76145930 -0.2097568 0.0000038
## Radish-Avocado      0.04960726 -0.39792128  0.4971358 0.9999758
## Kiwifruit-Carrot   -1.33104222 -1.55601830 -1.1060661 0.0000000
## Onion-Carrot        0.05340074 -0.18824037  0.2950418 0.9976312
## Pak Choi-Carrot    -0.55577367 -0.81396479 -0.2975825 0.0000000
## Pear-Carrot        -1.37425052 -1.63465708 -1.1138440 0.0000000
## Radish-Carrot      -0.83903520 -1.27721263 -0.4008578 0.0000003
## Onion-Kiwifruit     1.38444296  1.12445640  1.6444295 0.0000000
## Pak Choi-Kiwifruit  0.77526855  0.49983210  1.0507050 0.0000000
## Pear-Kiwifruit     -0.04320830 -0.32072256  0.2343060 0.9997592
## Radish-Kiwifruit    0.49200702  0.04345151  0.9405625 0.0202976
## Pak Choi-Onion     -0.60917441 -0.89838269 -0.3199661 0.0000000
## Pear-Onion         -1.42765126 -1.71883909 -1.1364634 0.0000000
## Radish-Onion       -0.89243594 -1.34957731 -0.4352946 0.0000002
## Pear-Pak Choi      -0.81847685 -1.12353845 -0.5134153 0.0000000
## Radish-Pak Choi    -0.28326153 -0.74936288  0.1828398 0.5851131
## Radish-Pear         0.53521532  0.06788312  1.0025475 0.0124546
## 
## $Region
##                                    diff         lwr        upr     p adj
## Bay of Plenty-Auckland     0.5037846203 -0.25646963 1.26403887 0.5664457
## Canterbury-Auckland        0.4975456275 -0.25361367 1.24870492 0.5671217
## Hawkes Bay-Auckland        0.4891334778 -0.27315471 1.25142167 0.6164521
## Marlborough-Auckland       0.4501978508 -0.35775827 1.25815397 0.7996824
## Northland-Auckland         0.5565471196 -0.26647708 1.37957132 0.5339770
## Otago-Auckland             0.7180466488 -0.17192842 1.60802171 0.2545594
## Poverty Bay-Auckland       0.0411383459 -0.86579015 0.94806684 1.0000000
## Southland-Auckland         0.2253849296 -0.66459014 1.11536000 0.9995706
## Tasman-Auckland            0.4984162280 -0.26419724 1.26102969 0.5881703
## Wairarapa-Auckland         0.3211508027 -0.56882426 1.21112587 0.9897996
## Whangarei-Auckland         0.4087863216 -0.54720025 1.36477289 0.9620413
## Canterbury-Bay of Plenty  -0.0062389928 -0.21962697 0.20714898 1.0000000
## Hawkes Bay-Bay of Plenty  -0.0146511425 -0.26440586 0.23510358 1.0000000
## Marlborough-Bay of Plenty -0.0535867695 -0.41976600 0.31259246 0.9999983
## Northland-Bay of Plenty    0.0527624993 -0.34556271 0.45108771 0.9999994
## Otago-Bay of Plenty        0.2142620285 -0.30856658 0.73709064 0.9722510
## Poverty Bay-Bay of Plenty -0.4626462744 -1.01383901 0.08854646 0.2018923
## Southland-Bay of Plenty   -0.2783996907 -0.80122830 0.24442892 0.8433917
## Tasman-Bay of Plenty      -0.0053683923 -0.25611416 0.24537737 1.0000000
## Wairarapa-Bay of Plenty   -0.1826338176 -0.70546243 0.34019479 0.9922072
## Whangarei-Bay of Plenty   -0.0949982987 -0.72365120 0.53365460 0.9999976
## Hawkes Bay-Canterbury     -0.0084121497 -0.22893693 0.21211263 1.0000000
## Marlborough-Canterbury    -0.0473477767 -0.39424976 0.29955421 0.9999992
## Northland-Canterbury       0.0590014921 -0.32167780 0.43968079 0.9999969
## Otago-Canterbury           0.2205010213 -0.28901199 0.73001404 0.9586172
## Poverty Bay-Canterbury    -0.4564072816 -0.99498614 0.08217158 0.1900762
## Southland-Canterbury      -0.2721606980 -0.78167371 0.23735232 0.8406182
## Tasman-Canterbury          0.0008706005 -0.22077595 0.22251716 1.0000000
## Wairarapa-Canterbury      -0.1763948248 -0.68590784 0.33311819 0.9927713
## Whangarei-Canterbury      -0.0887593059 -0.70638235 0.52886374 0.9999986
## Marlborough-Hawkes Bay    -0.0389356270 -0.40931919 0.33144793 1.0000000
## Northland-Hawkes Bay       0.0674136418 -0.33477999 0.46960728 0.9999931
## Otago-Hawkes Bay           0.2289131710 -0.29686863 0.75469498 0.9568030
## Poverty Bay-Hawkes Bay    -0.4479951319 -1.00198988 0.10599962 0.2513553
## Southland-Hawkes Bay      -0.2637485483 -0.78953035 0.26203326 0.8896134
## Tasman-Hawkes Bay          0.0092827502 -0.24756388 0.26612938 1.0000000
## Wairarapa-Hawkes Bay      -0.1679826751 -0.69376448 0.35779913 0.9963729
## Whangarei-Hawkes Bay      -0.0803471562 -0.71145825 0.55076394 0.9999996
## Northland-Marlborough      0.1063492688 -0.37683758 0.58953612 0.9998903
## Otago-Marlborough          0.2678487980 -0.32219892 0.85789652 0.9421845
## Poverty Bay-Marlborough   -0.4090595049 -1.02438055 0.20626154 0.5613299
## Southland-Marlborough     -0.2248129212 -0.81486064 0.36523480 0.9841871
## Tasman-Marlborough         0.0482183772 -0.32283418 0.41927093 0.9999995
## Wairarapa-Marlborough     -0.1290470481 -0.71909477 0.46100067 0.9998970
## Whangarei-Marlborough     -0.0414115292 -0.72698503 0.64416197 1.0000000
## Otago-Northland            0.1614995292 -0.44901833 0.77201739 0.9993506
## Poverty Bay-Northland     -0.5154087737 -1.15038573 0.11956818 0.2461470
## Southland-Northland       -0.3311621901 -0.94168005 0.27935567 0.8264971
## Tasman-Northland          -0.0581308916 -0.46094069 0.34467891 0.9999985
## Wairarapa-Northland       -0.2353963170 -0.84591418 0.37512154 0.9826308
## Whangarei-Northland       -0.1477607980 -0.85102943 0.55550783 0.9999309
## Poverty Bay-Otago         -0.6769083029 -1.39654885 0.04273225 0.0877449
## Southland-Otago           -0.4926617192 -1.19081560 0.20549216 0.4643080
## Tasman-Otago              -0.2196304208 -0.74588371 0.30662287 0.9682190
## Wairarapa-Otago           -0.3968958461 -1.09504973 0.30125803 0.7779120
## Whangarei-Otago           -0.3092603272 -1.08982010 0.47129944 0.9785356
## Southland-Poverty Bay      0.1842465837 -0.53539396 0.90388713 0.9995235
## Tasman-Poverty Bay         0.4572778821 -0.09716436 1.01172013 0.2245281
## Wairarapa-Poverty Bay      0.2800124568 -0.43962809 0.99965301 0.9813450
## Whangarei-Poverty Bay      0.3676479757 -0.43218778 1.16748373 0.9370607
## Tasman-Southland           0.2730312985 -0.25322199 0.79928459 0.8650442
## Wairarapa-Southland        0.0957658731 -0.60238801 0.79391975 0.9999991
## Whangarei-Southland        0.1834013921 -0.59715838 0.96396116 0.9997917
## Wairarapa-Tasman          -0.1772654254 -0.70351871 0.34898786 0.9942689
## Whangarei-Tasman          -0.0896299064 -0.72113385 0.54187404 0.9999988
## Whangarei-Wairarapa        0.0876355189 -0.69292425 0.86819529 0.9999999
#region within a crop
#I did this for beta and then decided I prefer it because of the biased sampling across crops/regions
#radish has only one location
categories_crop <- meta_data %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
#filtered_sppr <- sppr[grepl('^Apple', names(sppr))]
filtered_shannondiv <- shannondiv[grepl(paste0('^',item), names(shannondiv))]
filtered_meta_data<-meta_data %>% filter(sample_ID %in% names(filtered_shannondiv))
shannondiv_aov <-aov(filtered_shannondiv ~ Region, data = filtered_meta_data)
print(summary(shannondiv_aov))
print(TukeyHSD(shannondiv_aov))
tukey_test<-TukeyHSD(shannondiv_aov)
tukey_test<-tukey_test$Region
write.csv(tukey_test,paste('Alpha_shannon_diversity_allreplicates_',item,'vsregion.csv'),quote = F,row.names = T)
}
## [1] "Analysing Apple"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1  0.007 0.00691   0.049  0.825
## Residuals   65  9.082 0.13973               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                          diff       lwr     upr     p adj
## Tasman-Hawkes Bay -0.02117451 -0.211389 0.16904 0.8247621
## 
## [1] "Analysing Pear"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1  0.583  0.5828    1.91  0.175
## Residuals   38 11.594  0.3051               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                        diff        lwr       upr    p adj
## Tasman-Hawkes Bay -0.263393 -0.6492039 0.1224178 0.175026
## 
## [1] "Analysing Avocado"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       2  0.153 0.07655   0.306  0.738
## Residuals   62 15.527 0.25044               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                                diff        lwr       upr     p adj
## Northland-Bay of Plenty  0.10007883 -0.2453594 0.4455170 0.7668904
## Whangarei-Bay of Plenty -0.04768197 -0.5721437 0.4767798 0.9740791
## Whangarei-Northland     -0.14776080 -0.7183943 0.4228727 0.8087646
## 
## [1] "Analysing Kiwifruit"
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Region       3  3.966  1.3221   4.926 0.00404 **
## Residuals   59 15.836  0.2684                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                                  diff         lwr        upr     p adj
## Hawkes Bay-Bay of Plenty  -0.44076165 -0.95755841 0.07603511 0.1206497
## Poverty Bay-Bay of Plenty -0.52474895 -1.06616873 0.01667082 0.0607565
## Tasman-Bay of Plenty       0.18879651 -0.25010190 0.62769492 0.6681835
## Poverty Bay-Hawkes Bay    -0.08398731 -0.74953979 0.58156518 0.9870700
## Tasman-Hawkes Bay          0.62955816  0.04436028 1.21475604 0.0302723
## Tasman-Poverty Bay         0.71354546  0.10649272 1.32059821 0.0149710
## 
## [1] "Analysing Pakchoi"
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Region       3  2.433  0.8110   3.332 0.0298 *
## Residuals   37  9.007  0.2434                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                             diff         lwr       upr     p adj
## Canterbury-Auckland    0.5910218 -0.12363480 1.3056784 0.1354441
## Hawkes Bay-Auckland    0.9346287 -0.07894837 1.9482058 0.0798167
## Southland-Auckland     0.2253849 -0.57209327 1.0228631 0.8717124
## Hawkes Bay-Canterbury  0.3436070 -0.46725473 1.1544686 0.6676674
## Southland-Canterbury  -0.3656368 -0.88151436 0.1502407 0.2429283
## Southland-Hawkes Bay  -0.7092438 -1.59396644 0.1754788 0.1546168
## 
## [1] "Analysing Onion"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3  0.495  0.1652   0.909  0.444
## Residuals   46  8.359  0.1817               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                               diff        lwr       upr     p adj
## Marlborough-Canterbury -0.09754088 -0.4816738 0.2865921 0.9053416
## Otago-Canterbury        0.12255610 -0.4642167 0.7093289 0.9442408
## Wairarapa-Canterbury   -0.22658792 -0.7056859 0.2525100 0.5921865
## Otago-Marlborough       0.22009698 -0.3453314 0.7855254 0.7284405
## Wairarapa-Marlborough  -0.12904705 -0.5817521 0.3236580 0.8719835
## Wairarapa-Otago        -0.34914403 -0.9829310 0.2846430 0.4645013
## 
## [1] "Analysing Carrot"
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Region       1  0.375  0.3753   3.605 0.0608 .
## Residuals   89  9.265  0.1041                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data)
## 
## $Region
##                      diff         lwr       upr    p adj
## Otago-Canterbury 0.313265 -0.01456642 0.6410964 0.060845
#impact of flowering time
shannondiv_aov <-aov(shannondiv ~ flowertime, data = meta_data)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## flowertime    1  150.6  150.62   491.2 <2e-16 ***
## Residuals   426  130.6    0.31                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#TukeyHSD(shannondiv_aov)

#can plot that out
shannondiv_df <- shannondiv %>% 
  enframe() %>% 
  full_join(meta_data, by = c("name" = "sample_ID"))

#shannondiv_df$Crop <- factor(shannondiv_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
#brads order
shannondiv_df$Crop <- factor(shannondiv_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

#just crop
plot_sppr <- ggplot(shannondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  scale_fill_manual(values=col_bound)+
  theme_bw()+
  labs(x = "Crop",
       y = "Shannon",
       title = "Species richness: shannon")
plot_sppr

#other significant factor was region
plot_sppr <- ggplot(shannondiv_df, aes(x = Region, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~Crop, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Shannon",
       title = "Species richness: shannon")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

#other significant factor was flowertime
plot_sppr <- ggplot(shannondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~flowertime, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Shannon",
       title = "Species richness: shannon")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

Beta diversity and MDS plots

I have to remove the 0’s (e.g. samples where there was no non-bee insects observed ~mostly apple/pear) but here it is: After reading this https://stats.stackexchange.com/questions/639594/permanova-with-vegan-adonis-for-three-factors-and-repeated-measures im trying to avoid us having to look at multi factors for beta diversity but we can chat about that if needed. For now I’ve removed them.

################
#beta diversity#
################

#remove zero values:
counts_wide_no0 <- counts_wide[rowSums(counts_wide == 0) != ncol(counts_wide), ]

meta_data_no0<-meta_data %>% filter(sample_ID %in% row.names(counts_wide_no0))

#crop
perm<-adonis2(counts_wide_no0 ~ Crop,data=meta_data_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_wide_no0 ~ Crop, data = meta_data_no0)
##           Df SumOfSqs      R2    F Pr(>F)    
## Model      7   38.567 0.24426 17.5  0.001 ***
## Residual 379  119.325 0.75574                
## Total    386  157.892 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pairwise_adonis<-pairwise.adonis2(counts_wide_no0 ~ Crop, data = meta_data_no0)
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 
for (item in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }

write.csv(myData,'Pairwise_adonis_beta_diversity_allreplicates_countsvscrops.csv',quote = F,row.names = F)

#flowertime
perm<-adonis2(counts_wide_no0 ~ flowertime,data=meta_data_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_wide_no0 ~ flowertime, data = meta_data_no0)
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1   17.958 0.11374 49.408  0.001 ***
## Residual 385  139.934 0.88626                  
## Total    386  157.892 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#pairwise_adonis<-pairwise.adonis2(counts_wide_no0 ~ flowertime, data = meta_data_no0)
#pairwise_adonis

#I think to look at the impact of region within a crop we need to run this within a crop. This is because there is little overlap in regions between crops and the 0,0 situation makes it too confusing (at least for me)
#so just testing does community composition vary between regions within a crop. 
categories_crop <- meta_data %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
filtered_counts_wide_no0 <- counts_wide_no0[grepl(paste0('^',item), rownames(counts_wide_no0)), ] %>% select(where(~ sum(.) != 0))
filtered_meta_data_no0<-meta_data %>% filter(sample_ID %in% row.names(filtered_counts_wide_no0))
perm<-adonis2(filtered_counts_wide_no0 ~ Region,data=filtered_meta_data_no0)
print(perm)
pairwise_adonis<-pairwise.adonis2(filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
print(pairwise_adonis)
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 
for (item1 in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item1,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item1, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }
write.csv(myData,paste0('Pairwise_adonis_beta_diversity_allreplicates_regionvs',item,'.csv'),quote = F,row.names = F)
}
## [1] "Analysing Apple"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.1146 0.13658 5.6945  0.001 ***
## Residual 36  13.3686 0.86342                  
## Total    37  15.4832 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.1146 0.13658 5.6945  0.001 ***
## Residual 36  13.3686 0.86342                  
## Total    37  15.4832 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pear"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.3614 0.19972 7.7365  0.001 ***
## Residual 31   9.4620 0.80028                  
## Total    32  11.8234 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.3614 0.19972 7.7365  0.001 ***
## Residual 31   9.4620 0.80028                  
## Total    32  11.8234 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Avocado"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     2    1.984 0.09929 3.4172  0.001 ***
## Residual 62   17.999 0.90071                  
## Total    64   19.983 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Northland`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.6091 0.08728 5.4506  0.001 ***
## Residual 57  16.8276 0.91272                  
## Total    58  18.4368 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Bay of Plenty_vs_Whangarei`
##          Df SumOfSqs    R2     F Pr(>F)
## Model     1   0.3286 0.023 1.083  0.357
## Residual 46  13.9555 0.977             
## Total    47  14.2841 1.000             
## 
## $Northland_vs_Whangarei
##          Df SumOfSqs      R2     F Pr(>F)   
## Model     1   0.7935 0.13209 3.196  0.002 **
## Residual 21   5.2140 0.86791                
## Total    22   6.0075 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Kiwifruit"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   3.0124 0.14317 3.0077  0.001 ***
## Residual 54  18.0280 0.85683                  
## Total    57  21.0404 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Poverty Bay`
##          Df SumOfSqs      R2     F Pr(>F)
## Model     1   0.5005 0.03724 1.354  0.201
## Residual 35  12.9385 0.96276             
## Total    36  13.4390 1.00000             
## 
## $`Bay of Plenty_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   1.2985 0.09109 3.6079  0.002 **
## Residual 36  12.9567 0.90891                 
## Total    37  14.2552 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Bay of Plenty_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   1.3003 0.08221 3.8516  0.002 **
## Residual 43  14.5169 0.91779                 
## Total    44  15.8172 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Poverty Bay_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.9114 0.20608 2.8553  0.026 *
## Residual 11   3.5112 0.79392                
## Total    12   4.4226 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Poverty Bay_vs_Tasman`
##          Df SumOfSqs     R2      F Pr(>F)  
## Model     1   0.6616 0.1154 2.3482  0.026 *
## Residual 18   5.0713 0.8846                
## Total    19   5.7329 1.0000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   1.1077 0.17874 4.1351  0.004 **
## Residual 19   5.0895 0.82126                 
## Total    20   6.1972 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pakchoi"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   2.5275 0.19973 3.0781  0.001 ***
## Residual 37  10.1273 0.80027                  
## Total    40  12.6548 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Auckland_vs_Canterbury
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.1250 0.13494 4.2115  0.001 ***
## Residual 27   7.2122 0.86506                  
## Total    28   8.3372 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Auckland_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3954 0.22901 1.4852  0.219
## Residual  5   1.3312 0.77099              
## Total     6   1.7265 1.00000              
## 
## $Auckland_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.9536 0.22868 3.2612  0.002 **
## Residual 11   3.2165 0.77132                 
## Total    12   4.1702 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Canterbury_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.5671 0.07584 2.1335  0.035 *
## Residual 26   6.9107 0.92416                
## Total    27   7.4778 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   0.9286 0.09549 3.3781  0.001 ***
## Residual 32   8.7961 0.90451                  
## Total    33   9.7247 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Southland`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.6992 0.19346 2.3987  0.024 *
## Residual 10   2.9150 0.80654                
## Total    11   3.6142 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Onion"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     3   1.6693 0.12486 2.1877  0.003 **
## Residual 46  11.6996 0.87514                 
## Total    49  13.3689 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs      R2     F Pr(>F)  
## Model     1   0.4072 0.09027 1.786  0.065 .
## Residual 18   4.1038 0.90973               
## Total    19   4.5110 1.00000               
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6737 0.07373 2.7063  0.005 **
## Residual 34   8.4640 0.92627                 
## Total    35   9.1378 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6049 0.09762 2.3801  0.006 **
## Residual 22   5.5909 0.90238                 
## Total    23   6.1958 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Otago_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3443 0.05336 1.3528  0.173
## Residual 24   6.1087 0.94664              
## Total    25   6.4530 1.00000              
## 
## $Otago_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.4502 0.12215 1.6698  0.065 .
## Residual 12   3.2355 0.87785                
## Total    13   3.6858 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Marlborough_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.7098 0.08546 2.6166  0.007 **
## Residual 28   7.5958 0.91454                 
## Total    29   8.3056 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Carrot"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.4846 0.02243 2.0424  0.029 *
## Residual 89  21.1162 0.97757                
## Total    90  21.6008 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.4846 0.02243 2.0424  0.025 *
## Residual 89  21.1162 0.97757                
## Total    90  21.6008 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"
#MDS
pk_NMDS <- metaMDS(counts_wide_no0,trymax=200)
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.1808728 
## Run 1 stress 0.1811676 
## ... Procrustes: rmse 0.02621764  max resid 0.3884263 
## Run 2 stress 0.1811394 
## ... Procrustes: rmse 0.03229755  max resid 0.3547199 
## Run 3 stress 0.1824531 
## Run 4 stress 0.181229 
## ... Procrustes: rmse 0.01533358  max resid 0.1312302 
## Run 5 stress 0.1810326 
## ... Procrustes: rmse 0.02088862  max resid 0.255728 
## Run 6 stress 0.1803824 
## ... New best solution
## ... Procrustes: rmse 0.01288165  max resid 0.1688527 
## Run 7 stress 0.1813274 
## Run 8 stress 0.1800154 
## ... New best solution
## ... Procrustes: rmse 0.01365318  max resid 0.2111474 
## Run 9 stress 0.1840188 
## Run 10 stress 0.1829942 
## Run 11 stress 0.1826043 
## Run 12 stress 0.1824726 
## Run 13 stress 0.1831849 
## Run 14 stress 0.1810535 
## Run 15 stress 0.1807829 
## Run 16 stress 0.1837965 
## Run 17 stress 0.1806178 
## Run 18 stress 0.1829143 
## Run 19 stress 0.1809495 
## Run 20 stress 0.1811565 
## Run 21 stress 0.1844161 
## Run 22 stress 0.183545 
## Run 23 stress 0.1805918 
## Run 24 stress 0.1828067 
## Run 25 stress 0.1807712 
## Run 26 stress 0.1815542 
## Run 27 stress 0.1808953 
## Run 28 stress 0.1816259 
## Run 29 stress 0.1825052 
## Run 30 stress 0.1834418 
## Run 31 stress 0.1813198 
## Run 32 stress 0.1810033 
## Run 33 stress 0.180476 
## ... Procrustes: rmse 0.01603817  max resid 0.2297263 
## Run 34 stress 0.1806056 
## Run 35 stress 0.1836544 
## Run 36 stress 0.1802247 
## ... Procrustes: rmse 0.01256078  max resid 0.1888376 
## Run 37 stress 0.1809401 
## Run 38 stress 0.1819349 
## Run 39 stress 0.1811702 
## Run 40 stress 0.1818746 
## Run 41 stress 0.1812262 
## Run 42 stress 0.1805689 
## Run 43 stress 0.1804361 
## ... Procrustes: rmse 0.009543539  max resid 0.1151564 
## Run 44 stress 0.1844368 
## Run 45 stress 0.1805961 
## Run 46 stress 0.1808972 
## Run 47 stress 0.1822709 
## Run 48 stress 0.1837717 
## Run 49 stress 0.1819097 
## Run 50 stress 0.1807142 
## Run 51 stress 0.1804499 
## ... Procrustes: rmse 0.02084664  max resid 0.3182816 
## Run 52 stress 0.1805091 
## ... Procrustes: rmse 0.02006514  max resid 0.3012419 
## Run 53 stress 0.1805673 
## Run 54 stress 0.183137 
## Run 55 stress 0.183093 
## Run 56 stress 0.1811575 
## Run 57 stress 0.1806697 
## Run 58 stress 0.1807773 
## Run 59 stress 0.1826691 
## Run 60 stress 0.1800559 
## ... Procrustes: rmse 0.006578451  max resid 0.08435007 
## Run 61 stress 0.1813459 
## Run 62 stress 0.1809039 
## Run 63 stress 0.1807454 
## Run 64 stress 0.1834446 
## Run 65 stress 0.1811507 
## Run 66 stress 0.1813321 
## Run 67 stress 0.1806274 
## Run 68 stress 0.1803747 
## ... Procrustes: rmse 0.006868104  max resid 0.06655475 
## Run 69 stress 0.1832643 
## Run 70 stress 0.1807037 
## Run 71 stress 0.1807365 
## Run 72 stress 0.180696 
## Run 73 stress 0.1810531 
## Run 74 stress 0.183301 
## Run 75 stress 0.1816265 
## Run 76 stress 0.1816314 
## Run 77 stress 0.1809428 
## Run 78 stress 0.1816406 
## Run 79 stress 0.181438 
## Run 80 stress 0.1802526 
## ... Procrustes: rmse 0.006846106  max resid 0.04939419 
## Run 81 stress 0.1807639 
## Run 82 stress 0.1816743 
## Run 83 stress 0.1835959 
## Run 84 stress 0.1804778 
## ... Procrustes: rmse 0.009564894  max resid 0.08641781 
## Run 85 stress 0.1810681 
## Run 86 stress 0.1803743 
## ... Procrustes: rmse 0.01376968  max resid 0.2030871 
## Run 87 stress 0.1821819 
## Run 88 stress 0.1809071 
## Run 89 stress 0.1827195 
## Run 90 stress 0.1803422 
## ... Procrustes: rmse 0.007660817  max resid 0.1087233 
## Run 91 stress 0.1816065 
## Run 92 stress 0.1817996 
## Run 93 stress 0.1840305 
## Run 94 stress 0.1805367 
## Run 95 stress 0.1801433 
## ... Procrustes: rmse 0.006559276  max resid 0.06968077 
## Run 96 stress 0.1801789 
## ... Procrustes: rmse 0.00681436  max resid 0.0749437 
## Run 97 stress 0.1805464 
## Run 98 stress 0.1802771 
## ... Procrustes: rmse 0.00916249  max resid 0.1215026 
## Run 99 stress 0.1819495 
## Run 100 stress 0.1809188 
## Run 101 stress 0.1841425 
## Run 102 stress 0.180598 
## Run 103 stress 0.180749 
## Run 104 stress 0.1810347 
## Run 105 stress 0.1804287 
## ... Procrustes: rmse 0.01292494  max resid 0.1967408 
## Run 106 stress 0.1817114 
## Run 107 stress 0.1803097 
## ... Procrustes: rmse 0.007336807  max resid 0.06503951 
## Run 108 stress 0.1814069 
## Run 109 stress 0.1809088 
## Run 110 stress 0.1810316 
## Run 111 stress 0.1830078 
## Run 112 stress 0.18391 
## Run 113 stress 0.1840016 
## Run 114 stress 0.1806406 
## Run 115 stress 0.1811548 
## Run 116 stress 0.1819363 
## Run 117 stress 0.1828125 
## Run 118 stress 0.1806492 
## Run 119 stress 0.1819042 
## Run 120 stress 0.1804936 
## ... Procrustes: rmse 0.01363679  max resid 0.1932652 
## Run 121 stress 0.1828812 
## Run 122 stress 0.180405 
## ... Procrustes: rmse 0.01896052  max resid 0.2957022 
## Run 123 stress 0.1841477 
## Run 124 stress 0.1808911 
## Run 125 stress 0.1805707 
## Run 126 stress 0.1835465 
## Run 127 stress 0.1824472 
## Run 128 stress 0.1830995 
## Run 129 stress 0.180322 
## ... Procrustes: rmse 0.01047497  max resid 0.1237494 
## Run 130 stress 0.180844 
## Run 131 stress 0.180687 
## Run 132 stress 0.1821559 
## Run 133 stress 0.1827156 
## Run 134 stress 0.1808858 
## Run 135 stress 0.1803782 
## ... Procrustes: rmse 0.009086889  max resid 0.0953232 
## Run 136 stress 0.1804617 
## ... Procrustes: rmse 0.01338299  max resid 0.2122864 
## Run 137 stress 0.1835722 
## Run 138 stress 0.1809247 
## Run 139 stress 0.1811366 
## Run 140 stress 0.1811599 
## Run 141 stress 0.1823912 
## Run 142 stress 0.1809011 
## Run 143 stress 0.1838304 
## Run 144 stress 0.1811361 
## Run 145 stress 0.1814769 
## Run 146 stress 0.1806402 
## Run 147 stress 0.1826331 
## Run 148 stress 0.1840076 
## Run 149 stress 0.1808494 
## Run 150 stress 0.1844777 
## Run 151 stress 0.1803724 
## ... Procrustes: rmse 0.009455823  max resid 0.1149696 
## Run 152 stress 0.1810778 
## Run 153 stress 0.1810257 
## Run 154 stress 0.1806025 
## Run 155 stress 0.180401 
## ... Procrustes: rmse 0.007509136  max resid 0.06634573 
## Run 156 stress 0.1816974 
## Run 157 stress 0.1807964 
## Run 158 stress 0.1807348 
## Run 159 stress 0.1804161 
## ... Procrustes: rmse 0.01267189  max resid 0.1870404 
## Run 160 stress 0.1822351 
## Run 161 stress 0.1820176 
## Run 162 stress 0.1824289 
## Run 163 stress 0.1834178 
## Run 164 stress 0.1807519 
## Run 165 stress 0.181611 
## Run 166 stress 0.1806634 
## Run 167 stress 0.1819039 
## Run 168 stress 0.1810552 
## Run 169 stress 0.1808497 
## Run 170 stress 0.1812339 
## Run 171 stress 0.1806493 
## Run 172 stress 0.1844967 
## Run 173 stress 0.1808117 
## Run 174 stress 0.1802321 
## ... Procrustes: rmse 0.01357134  max resid 0.2032147 
## Run 175 stress 0.1824363 
## Run 176 stress 0.1832015 
## Run 177 stress 0.1826769 
## Run 178 stress 0.182011 
## Run 179 stress 0.1822027 
## Run 180 stress 0.1825639 
## Run 181 stress 0.1806176 
## Run 182 stress 0.1803904 
## ... Procrustes: rmse 0.008902766  max resid 0.06830609 
## Run 183 stress 0.1805028 
## ... Procrustes: rmse 0.01374823  max resid 0.2102734 
## Run 184 stress 0.1814764 
## Run 185 stress 0.1806635 
## Run 186 stress 0.1814113 
## Run 187 stress 0.1823876 
## Run 188 stress 0.1826342 
## Run 189 stress 0.1817857 
## Run 190 stress 0.1828529 
## Run 191 stress 0.1808799 
## Run 192 stress 0.1806598 
## Run 193 stress 0.1807154 
## Run 194 stress 0.1820007 
## Run 195 stress 0.1843896 
## Run 196 stress 0.1821049 
## Run 197 stress 0.1805543 
## Run 198 stress 0.1818226 
## Run 199 stress 0.180387 
## ... Procrustes: rmse 0.00817496  max resid 0.1000537 
## Run 200 stress 0.1807325 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##    198: no. of iterations >= maxit
##      2: stress ratio > sratmax
#doesnt converge - whats up?
pk_NMDS
## 
## Call:
## metaMDS(comm = counts_wide_no0, trymax = 200) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     wisconsin(sqrt(counts_wide_no0)) 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.1800154 
## Stress type 1, weak ties
## Best solution was not repeated after 200 tries
## The best solution was from try 8 (random start)
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'wisconsin(sqrt(counts_wide_no0))'
#stress (ideally <0.2)

stressplot(pk_NMDS)

plot(pk_NMDS)

plot_df <- scores(pk_NMDS, display = "sites") %>% 
  as.data.frame() %>% 
  rownames_to_column("site") %>% 
  full_join(meta_data_no0, by = c("site" = "sample_ID"))

#plot_df$Crop <- factor(plot_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
plot_df$Crop <- factor(plot_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))


plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 0.8) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  labs(title = "NMDS")
plot_nmds

Beta and MDS plot

this is a little arbitary but means the MDS plot isnt trying to converge stuff with basically no data

Eddy to look at pairwise.adonis for pairwise data (note that this seems to be a very different statistical test to what vegans::adonis2 does … need to read)

#################################################################
#setting a minimum count of 5 across at least 2 taxa#
#################################################################
#remove <5 counts:
counts_wide_no0 <- counts_wide[rowSums(counts_wide) >= 5, ]
#remove things that are only from a single species
counts_wide_no0 <-counts_wide_no0[which(rowSums(1*(counts_wide_no0!=0))>1),]

meta_data_no0<-meta_data %>% filter(sample_ID %in% row.names(counts_wide_no0))

#redoing the beta as well
#crop
perm<-adonis2(counts_wide_no0 ~ Crop,data=meta_data_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_wide_no0 ~ Crop, data = meta_data_no0)
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      7    32.76 0.27146 16.448  0.001 ***
## Residual 309    87.92 0.72854                  
## Total    316   120.68 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pairwise_adonis<-pairwise.adonis2(counts_wide_no0 ~ Crop, data = meta_data_no0)
pairwise_adonis
## $parent_call
## [1] "counts_wide_no0 ~ Crop , strata = Null , permutations 999"
## 
## $Apple_vs_Pear
##          Df SumOfSqs     R2      F Pr(>F)  
## Model     1   0.8318 0.0885 2.6214  0.025 *
## Residual 27   8.5672 0.9115                
## Total    28   9.3990 1.0000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Avocado
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.1888 0.09188 7.0824  0.001 ***
## Residual 70  21.6331 0.90812                  
## Total    71  23.8218 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Kiwifruit
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1   1.7879 0.1133 5.3665  0.001 ***
## Residual 42  13.9925 0.8867                  
## Total    43  15.7803 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Apple_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.5589 0.13831 7.8648  0.001 ***
## Residual 49  15.9426 0.86169                  
## Total    50  18.5015 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.3709 0.17073 3.9116  0.001 ***
## Residual 19   6.6587 0.82927                  
## Total    20   8.0295 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.8667 0.15224 10.236  0.001 ***
## Residual 57  15.9637 0.84776                  
## Total    58  18.8304 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1   3.1209 0.11142 12.414  0.001 ***
## Residual  99  24.8886 0.88858                  
## Total    100  28.0095 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Avocado
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1   2.1075 0.0819 7.0474  0.001 ***
## Residual 79  23.6247 0.9181                  
## Total    80  25.7322 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Kiwifruit
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   3.1454 0.16443 10.036  0.001 ***
## Residual 51  15.9841 0.83557                  
## Total    52  19.1295 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pear_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   4.3787 0.19624 14.161  0.001 ***
## Residual 58  17.9342 0.80376                  
## Total    59  22.3129 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Radish
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1   1.9700 0.1855 6.3767  0.001 ***
## Residual 28   8.6503 0.8145                  
## Total    29  10.6204 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   4.6645 0.20621 17.146  0.001 ***
## Residual 66  17.9554 0.79379                  
## Total    67  22.6199 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Carrot
##           Df SumOfSqs     R2      F Pr(>F)    
## Model      1    4.904 0.1543 19.705  0.001 ***
## Residual 108   26.880 0.8457                  
## Total    109   31.785 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Kiwifruit
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    4.209 0.12655 13.619  0.001 ***
## Residual 94   29.050 0.87345                  
## Total    95   33.259 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Avocado_vs_Pak Choi`
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1    7.603 0.19696 24.772  0.001 ***
## Residual 101   31.000 0.80304                  
## Total    102   38.603 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    2.799 0.11417 9.1512  0.001 ***
## Residual 71   21.716 0.88583                  
## Total    72   24.515 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Onion
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1    8.804 0.22106 30.934  0.001 ***
## Residual 109   31.021 0.77894                  
## Total    110   39.825 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1   10.482 0.20786 39.622  0.001 ***
## Residual 151   39.946 0.79214                  
## Total    152   50.428 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Kiwifruit_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   5.3218 0.18555 16.631  0.001 ***
## Residual 73  23.3595 0.81445                  
## Total    74  28.6813 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.6203 0.10323 4.9499  0.001 ***
## Residual 43  14.0756 0.89677                  
## Total    44  15.6959 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Onion
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1   6.6986 0.2227 23.207  0.001 ***
## Residual 81  23.3806 0.7773                  
## Total    82  30.0792 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Carrot
##           Df SumOfSqs     R2     F Pr(>F)    
## Model      1    8.759 0.2133 33.35  0.001 ***
## Residual 123   32.305 0.7867                 
## Total    124   41.065 1.0000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Radish`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.7268 0.04338 2.2675  0.012 *
## Residual 50  16.0257 0.95662                
## Total    51  16.7524 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Onion`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   3.5642 0.12335 12.382  0.001 ***
## Residual 88  25.3307 0.87665                  
## Total    89  28.8950 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Carrot`
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1    5.785 0.14449 21.956  0.001 ***
## Residual 130   34.256 0.85551                  
## Total    131   40.041 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Radish_vs_Onion
##          Df SumOfSqs    R2      F Pr(>F)    
## Model     1   1.6844 0.095 6.0881  0.001 ***
## Residual 58  16.0468 0.905                  
## Total    59  17.7312 1.000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Radish_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1    2.043 0.07562 8.1811  0.001 ***
## Residual 100   24.972 0.92438                  
## Total    101   27.015 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Onion_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1    3.128 0.08362 12.592  0.001 ***
## Residual 138   34.277 0.91638                  
## Total    139   37.404 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 
for (item in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }

write.csv(myData,'Pairwise_adonis_beta_diversity_allreplicates_countsvscrops_mincount5.csv',quote = F,row.names = F)

#flowertime
perm<-adonis2(counts_wide_no0 ~ flowertime,data=meta_data_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_wide_no0 ~ flowertime, data = meta_data_no0)
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1   15.008 0.12436 44.737  0.001 ***
## Residual 315  105.672 0.87564                  
## Total    316  120.679 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#pairwise_adonis<-pairwise.adonis2(counts_wide_no0 ~ flowertime, data = meta_data_no0)
#pairwise_adonis

#I think to look at the impact of region within a crop we need to run this within a crop. This is because there is little overlap in regions between crops and the 0,0 situation makes it too confusing (at least for me)
#so just testing does community composition vary between regions within a crop. 
categories_crop <- meta_data %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
filtered_counts_wide_no0 <- counts_wide_no0[grepl(paste0('^',item), rownames(counts_wide_no0)), ] %>% select(where(~ sum(.) != 0))
filtered_meta_data_no0<-meta_data %>% filter(sample_ID %in% row.names(filtered_counts_wide_no0))
perm<-adonis2(filtered_counts_wide_no0 ~ Region,data=filtered_meta_data_no0)
print(perm)
pairwise_adonis<-pairwise.adonis2(filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
print(pairwise_adonis)
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 
for (item1 in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item1,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item1, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }
write.csv(myData,paste0('Pairwise_adonis_beta_diversity_allreplicates_regionvs',item,'_mincount5.csv'),quote = F,row.names = F)
}
## [1] "Analysing Apple"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.8974 0.27295 3.0034  0.025 *
## Residual  8   2.3904 0.72705                
## Total     9   3.2878 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.8974 0.27295 3.0034  0.023 *
## Residual  8   2.3904 0.72705                
## Total     9   3.2878 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pear"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.0378 0.38599 10.687  0.001 ***
## Residual 17   3.2416 0.61401                  
## Total    18   5.2794 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.0378 0.38599 10.687  0.001 ***
## Residual 17   3.2416 0.61401                  
## Total    18   5.2794 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Avocado"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     2   1.9528 0.10645 3.5144  0.001 ***
## Residual 59  16.3924 0.89355                  
## Total    61  18.3453 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Northland`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.6054 0.09541 5.6954  0.001 ***
## Residual 54  15.2215 0.90459                  
## Total    55  16.8269 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Bay of Plenty_vs_Whangarei`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3023 0.02304 1.0377  0.384
## Residual 44  12.8187 0.97696              
## Total    45  13.1210 1.00000              
## 
## $Northland_vs_Whangarei
##          Df SumOfSqs      R2     F Pr(>F)   
## Model     1   0.7848 0.14193 3.308  0.002 **
## Residual 20   4.7447 0.85807                
## Total    21   5.5295 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Kiwifruit"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   2.2596 0.21109 2.6757  0.001 ***
## Residual 30   8.4450 0.78891                  
## Total    33  10.7047 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 'nperm' >= set of all permutations: complete enumeration.
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Poverty Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.1740 0.02843 0.5561  0.853
## Residual 19   5.9439 0.97157              
## Total    20   6.1178 1.00000              
## 
## $`Bay of Plenty_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.5780 0.08791 1.8312  0.076 .
## Residual 19   5.9973 0.91209                
## Total    20   6.5754 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Bay of Plenty_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.6250 0.17188 5.8116  0.001 ***
## Residual 28   7.8291 0.82812                  
## Total    29   9.4540 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Poverty Bay_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1  0.35776 0.36742 1.1616 0.6667
## Residual  2  0.61596 0.63258              
## Total     3  0.97372 1.00000              
## 
## $`Poverty Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1  0.52261 0.17595 2.3486  0.049 *
## Residual 11  2.44769 0.82405                
## Total    12  2.97030 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1  0.31247 0.11106 1.3742   0.19
## Residual 11  2.50115 0.88894              
## Total    12  2.81362 1.00000              
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pakchoi"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   2.5275 0.19973 3.0781  0.001 ***
## Residual 37  10.1273 0.80027                  
## Total    40  12.6548 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Auckland_vs_Canterbury
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.1250 0.13494 4.2115  0.001 ***
## Residual 27   7.2122 0.86506                  
## Total    28   8.3372 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Auckland_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3954 0.22901 1.4852   0.23
## Residual  5   1.3312 0.77099              
## Total     6   1.7265 1.00000              
## 
## $Auckland_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.9536 0.22868 3.2612  0.004 **
## Residual 11   3.2165 0.77132                 
## Total    12   4.1702 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Canterbury_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.5671 0.07584 2.1335  0.025 *
## Residual 26   6.9107 0.92416                
## Total    27   7.4778 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   0.9286 0.09549 3.3781  0.001 ***
## Residual 32   8.7961 0.90451                  
## Total    33   9.7247 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Southland`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.6992 0.19346 2.3987  0.039 *
## Residual 10   2.9150 0.80654                
## Total    11   3.6142 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Onion"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   1.6258 0.12826 2.2069  0.001 ***
## Residual 45  11.0501 0.87174                  
## Total    48  12.6759 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs      R2     F Pr(>F)  
## Model     1   0.4072 0.09027 1.786   0.04 *
## Residual 18   4.1038 0.90973               
## Total    19   4.5110 1.00000               
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6737 0.07373 2.7063  0.008 **
## Residual 34   8.4640 0.92627                 
## Total    35   9.1378 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.5556 0.10107 2.3612  0.017 *
## Residual 21   4.9415 0.89893                
## Total    22   5.4971 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Otago_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3443 0.05336 1.3528  0.185
## Residual 24   6.1087 0.94664              
## Total    25   6.4530 1.00000              
## 
## $Otago_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1  0.42663 0.14161 1.8147  0.073 .
## Residual 11  2.58610 0.85839                
## Total    12  3.01273 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Marlborough_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6893 0.09027 2.6792  0.007 **
## Residual 27   6.9463 0.90973                 
## Total    28   7.6356 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Carrot"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_wide_no0 ~ Region, data = filtered_meta_data_no0)
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.4846 0.02243 2.0424  0.026 *
## Residual 89  21.1162 0.97757                
## Total    90  21.6008 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.4846 0.02243 2.0424  0.026 *
## Residual 89  21.1162 0.97757                
## Total    90  21.6008 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"
#MDS
pk_NMDS <- metaMDS(counts_wide_no0,trymax=200)
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.2400227 
## Run 1 stress 0.239265 
## ... New best solution
## ... Procrustes: rmse 0.009357989  max resid 0.1174194 
## Run 2 stress 0.2392672 
## ... Procrustes: rmse 0.0006387654  max resid 0.004872033 
## ... Similar to previous best
## Run 3 stress 0.2516446 
## Run 4 stress 0.266451 
## Run 5 stress 0.2397358 
## ... Procrustes: rmse 0.009537836  max resid 0.1081885 
## Run 6 stress 0.2392506 
## ... New best solution
## ... Procrustes: rmse 0.001149388  max resid 0.0133466 
## Run 7 stress 0.2388986 
## ... New best solution
## ... Procrustes: rmse 0.006357251  max resid 0.1082031 
## Run 8 stress 0.2389823 
## ... Procrustes: rmse 0.007794151  max resid 0.1081838 
## Run 9 stress 0.2389776 
## ... Procrustes: rmse 0.007958551  max resid 0.108157 
## Run 10 stress 0.2387175 
## ... New best solution
## ... Procrustes: rmse 0.004613259  max resid 0.07844746 
## Run 11 stress 0.2658607 
## Run 12 stress 0.264785 
## Run 13 stress 0.2400242 
## Run 14 stress 0.2400349 
## Run 15 stress 0.238737 
## ... Procrustes: rmse 0.0006361614  max resid 0.01008383 
## Run 16 stress 0.2406161 
## Run 17 stress 0.2389822 
## ... Procrustes: rmse 0.006174256  max resid 0.1084564 
## Run 18 stress 0.2389818 
## ... Procrustes: rmse 0.006725444  max resid 0.1083825 
## Run 19 stress 0.2392642 
## Run 20 stress 0.2389683 
## ... Procrustes: rmse 0.006274827  max resid 0.108424 
## Run 21 stress 0.2392678 
## Run 22 stress 0.2700179 
## Run 23 stress 0.2396245 
## Run 24 stress 0.2387175 
## ... Procrustes: rmse 0.0002217419  max resid 0.002766253 
## ... Similar to previous best
## *** Best solution repeated 1 times
#converges - yay - a little stressed but is a lot of data
pk_NMDS
## 
## Call:
## metaMDS(comm = counts_wide_no0, trymax = 200) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     wisconsin(sqrt(counts_wide_no0)) 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.2387175 
## Stress type 1, weak ties
## Best solution was repeated 1 time in 24 tries
## The best solution was from try 10 (random start)
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'wisconsin(sqrt(counts_wide_no0))'
#stress 
stressplot(pk_NMDS)

plot(pk_NMDS)

plot_df <- scores(pk_NMDS, display = "sites") %>% 
  as.data.frame() %>% 
  rownames_to_column("site") %>% 
  full_join(meta_data_no0, by = c("site" = "sample_ID"))

#plot_df$Crop <- factor(plot_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
plot_df$Crop <- factor(plot_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 1) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 3, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 15, "Apple" = 15, "Pear" = 15, "Kiwifruit" = 15, "Pak Choi" = 16, "Radish" = 16, "Carrot" = 16, "Onion" = 16))+
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 2, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 16, "Apple" = 16, "Pear" = 16, "Kiwifruit" = 16, "Pak Choi" = 15, "Radish" = 15, "Carrot" = 15, "Onion" = 15))+
  stat_ellipse(linetype = 1, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

# If you set the min at 6 it drops that outlier 

counts_wide_no0 <- counts_wide[rowSums(counts_wide) >= 6, ]
#remove things that are only from a single species
counts_wide_no0 <-counts_wide_no0[which(rowSums(1*(counts_wide_no0!=0))>1),]

meta_data_no0<-meta_data %>% filter(sample_ID %in% row.names(counts_wide_no0))
#MDS
pk_NMDS <- metaMDS(counts_wide_no0,trymax=200)
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.2381988 
## Run 1 stress 0.2367926 
## ... New best solution
## ... Procrustes: rmse 0.01308996  max resid 0.1264205 
## Run 2 stress 0.2438725 
## Run 3 stress 0.2449335 
## Run 4 stress 0.2528548 
## Run 5 stress 0.2415683 
## Run 6 stress 0.237678 
## Run 7 stress 0.2364148 
## ... New best solution
## ... Procrustes: rmse 0.006873202  max resid 0.1168836 
## Run 8 stress 0.2367782 
## ... Procrustes: rmse 0.006820162  max resid 0.116753 
## Run 9 stress 0.2400834 
## Run 10 stress 0.239276 
## Run 11 stress 0.2385495 
## Run 12 stress 0.2369731 
## Run 13 stress 0.2407344 
## Run 14 stress 0.2369696 
## Run 15 stress 0.2552951 
## Run 16 stress 0.23704 
## Run 17 stress 0.2367793 
## ... Procrustes: rmse 0.006836271  max resid 0.1167659 
## Run 18 stress 0.2364615 
## ... Procrustes: rmse 0.005007343  max resid 0.08420072 
## Run 19 stress 0.2367787 
## ... Procrustes: rmse 0.006832405  max resid 0.11677 
## Run 20 stress 0.2367931 
## ... Procrustes: rmse 0.006881784  max resid 0.1167787 
## Run 21 stress 0.2367785 
## ... Procrustes: rmse 0.006832947  max resid 0.1167458 
## Run 22 stress 0.2390624 
## Run 23 stress 0.2630538 
## Run 24 stress 0.2369718 
## Run 25 stress 0.2519962 
## Run 26 stress 0.2367925 
## ... Procrustes: rmse 0.006875294  max resid 0.1167854 
## Run 27 stress 0.2684408 
## Run 28 stress 0.2394014 
## Run 29 stress 0.2369713 
## Run 30 stress 0.2369701 
## Run 31 stress 0.2641893 
## Run 32 stress 0.2405361 
## Run 33 stress 0.2640991 
## Run 34 stress 0.2370671 
## Run 35 stress 0.2532711 
## Run 36 stress 0.2369773 
## Run 37 stress 0.2393817 
## Run 38 stress 0.2708643 
## Run 39 stress 0.2367926 
## ... Procrustes: rmse 0.006879341  max resid 0.1167896 
## Run 40 stress 0.2491148 
## Run 41 stress 0.2370465 
## Run 42 stress 0.2369703 
## Run 43 stress 0.2393935 
## Run 44 stress 0.2396342 
## Run 45 stress 0.239866 
## Run 46 stress 0.2364144 
## ... New best solution
## ... Procrustes: rmse 0.0006114514  max resid 0.006484654 
## ... Similar to previous best
## *** Best solution repeated 1 times
#converges - yay - a little stressed but is a lot of data
pk_NMDS
## 
## Call:
## metaMDS(comm = counts_wide_no0, trymax = 200) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     wisconsin(sqrt(counts_wide_no0)) 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.2364144 
## Stress type 1, weak ties
## Best solution was repeated 1 time in 46 tries
## The best solution was from try 46 (random start)
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'wisconsin(sqrt(counts_wide_no0))'
#stress 
stressplot(pk_NMDS)

plot(pk_NMDS)

plot_df <- scores(pk_NMDS, display = "sites") %>% 
  as.data.frame() %>% 
  rownames_to_column("site") %>% 
  full_join(meta_data_no0, by = c("site" = "sample_ID"))

#plot_df$Crop <- factor(plot_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
plot_df$Crop <- factor(plot_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 1) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds
## Warning in MASS::cov.trob(data[, vars]): Probable convergence failure

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 3, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 15, "Apple" = 15, "Pear" = 15, "Kiwifruit" = 15, "Pak Choi" = 16, "Radish" = 16, "Carrot" = 16, "Onion" = 16))+
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds
## Warning in MASS::cov.trob(data[, vars]): Probable convergence failure

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 2, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 16, "Apple" = 16, "Pear" = 16, "Kiwifruit" = 16, "Pak Choi" = 15, "Radish" = 15, "Carrot" = 15, "Onion" = 15))+
  stat_ellipse(linetype = 1, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds
## Warning in MASS::cov.trob(data[, vars]): Probable convergence failure

Inext

I next is designed to be robust to sampling so probably is very appropriate for this dataset

########
#innext#
########
library(iNEXT.3D)

#generate a list of the unique sample locations
categories <- unique(meta_data$Crop)
categories
## [1] "Apple"     "Pear"      "Avocado"   "Kiwifruit" "Pak Choi"  "Radish"   
## [7] "Onion"     "Carrot"
#create empty list
split_list <- list()
#fill empty list with a presence absence OTU from each location

head(allcrops_per_r_fly)
## # A tibble: 6 × 7
##   sample_ID          Ordering Region `Location and season` Day   Crop  raw_count
##   <chr>              <fct>    <chr>  <chr>                 <chr> <chr>     <dbl>
## 1 Apple_Hawkes Bay_… 32       Hawke… 104 Moteo-Pa road _2… Day_1 Apple         2
## 2 Apple_Hawkes Bay_… 33       Hawke… 104 Moteo-Pa road _2… Day_1 Apple         1
## 3 Apple_Hawkes Bay_… 34       Hawke… 104 Moteo-Pa road _2… Day_1 Apple        11
## 4 Apple_Hawkes Bay_… 54       Hawke… 104 Moteo-Pa road _2… Day_1 Apple         1
## 5 Apple_Hawkes Bay_… 34       Hawke… 104 Moteo-Pa road _2… Day_3 Apple        11
## 6 Apple_Hawkes Bay_… 14       Hawke… 185 Lawn road, Hasti… Day_1 Apple         1
#I need to fill up the NA's for the samples with 0 flies with some dummy 0 for ones species to keep them in the next stage - so this is just to add a 0 value in and I've chosed the insect '27' at random (cause i know it occurs in the dataset so Im not adding something not present)

#in '' as its a factor not a number
allcrops_per_r_fly_innext<-allcrops_per_r_fly
allcrops_per_r_fly_innext$Ordering[is.na(allcrops_per_r_fly_innext$Ordering)] <- '27'
allcrops_per_r_fly_innext$raw_count[is.na(allcrops_per_r_fly_innext$raw_count)] <- 0

#create empty list
split_list <- list()

for (category in categories) {
 print(category)
   subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == category)
#  subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == "Carrot")
  test<-subset_data %>% ungroup() %>% dplyr::select(sample_ID,Ordering,raw_count)
  test   <- test %>%  spread(key = sample_ID, value = raw_count)
  test<-test %>% remove_rownames %>% column_to_rownames(var="Ordering")
  test[is.na(test)] <- 0
 # test <- test[rowSums(test == 0) != ncol(test), ]
  test<-test %>% mutate(across(everything(), ~ ifelse(. > 0, 1, 0)))
  split_list[[category]]<-as(test,'matrix')
} 
## [1] "Apple"
## [1] "Pear"
## [1] "Avocado"
## [1] "Kiwifruit"
## [1] "Pak Choi"
## [1] "Radish"
## [1] "Onion"
## [1] "Carrot"
matrix_list <- list(data = list())
for (category in categories) {
  otu_table <- as(split_list[[category]], "matrix")
  matrix_list[["data"]][[category]] <- otu_table
}

#here Im setting the endpoint as NULL which is double the input sample size (this is probably most robust)
out.raw <- iNEXT3D(data = matrix_list$data, diversity = 'TD', q = c(0, 1, 2), datatype = 'incidence_raw', nboot = 50,endpoint=NULL)

#there is various ways to look at these plots:
#within a crop
ggiNEXT3D(out.raw, type = 1, facet.var = 'Assemblage') + facet_wrap(~Assemblage, nrow = 3)

#across crops
ggiNEXT3D(out.raw, type = 1, facet.var = "Order.q")
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 8 values. Consider specifying shapes manually if you need
##   that many of them.
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_point()`).

#sample completeness
ggiNEXT3D(out.raw, type = 2, facet.var = "Order.q", color.var = "Assemblage")
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 8 values. Consider specifying shapes manually if you need
##   that many of them.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

#running innext as abundance as well

#create empty list
split_list <- list()

for (category in categories) {
  print(category)
  subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == category)
 # subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == "Carrot")
  test<-subset_data %>% ungroup() %>% dplyr::select(sample_ID,Ordering,raw_count)
  test   <- test %>%  spread(key = sample_ID, value = raw_count)
  test<-test %>% remove_rownames %>% column_to_rownames(var="Ordering")
  test[is.na(test)] <- 0
  test<-as.data.frame(rowSums(test))
  colnames(test) <- c(paste0(category))
  # test <- test[rowSums(test == 0) != ncol(test), ]
 # test<-test %>% mutate(across(everything(), ~ ifelse(. > 0, 1, 0)))
  split_list[[category]]<-as(test,'matrix')
} 
## [1] "Apple"
## [1] "Pear"
## [1] "Avocado"
## [1] "Kiwifruit"
## [1] "Pak Choi"
## [1] "Radish"
## [1] "Onion"
## [1] "Carrot"
matrix_list <- list(data = list())
for (category in categories) {
  otu_table <- as(split_list[[category]], "matrix")
  matrix_list[["data"]][[category]] <- otu_table
}

#here Im setting the endpoint as NULL which is double the input sample size (this is probably most robust)
out.raw <- iNEXT3D(data = matrix_list$data, diversity = 'TD', q = c(0, 1, 2), datatype = 'abundance', nboot = 50,endpoint=NULL)

#there is various ways to look at these plots:
#within a sample
ggiNEXT3D(out.raw, type = 1, facet.var = 'Assemblage') + facet_wrap(~Assemblage, nrow = 3)

#across samples
ggiNEXT3D(out.raw, type = 1, facet.var = "Order.q")
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 8 values. Consider specifying shapes manually if you need
##   that many of them.
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_point()`).

#yes well I think we can agree across samples works better for apple and pear
#but its interesting that avocado diversity depending on how you look at it can change quite a bit

Doing analyses just on Day 1 samples to avoid the issue on uneven replication

I think this is probably the most conservative solution to this problem but Im happy to take other ideas

######################################################################
#conservatively taking only Day 1 samples and analysing
######################################################################


#conservatively if we just look at the first day due to all the biases in the data
meta_data_day1<-meta_data %>% filter(Day=='Day_1')

counts_day1<-allcrops_per_r_fly %>% ungroup() %>% filter(Day=='Day_1') %>% select(sample_ID,Ordering,raw_count)

#I need to fill up the NA's for the samples with 0 flies with some dummy 0 for ones species to keep them in the next stage - so this is just to add a 0 value in and I've chosed the insect '27' at random (cause i know it occurs in the dataset so Im not adding something not present)

#in '' as its a factor not a number
counts_day1$Ordering[is.na(counts_day1$Ordering)] <- '27'
counts_day1$raw_count[is.na(counts_day1$raw_count)] <- 0
#transform wide
counts_day1_wide<-counts_day1 %>% pivot_wider(names_from=`Ordering`,values_from = `raw_count`)
counts_day1_wide[is.na(counts_day1_wide)] <- 0
counts_day1_wide<-counts_day1_wide %>% remove_rownames %>% column_to_rownames(var="sample_ID")

#count species per property
sppr <- specnumber(counts_day1_wide)
head(sppr)
##        Apple_Hawkes Bay_104 Moteo-Pa road _2018_Day_1 
##                                                     4 
##    Apple_Hawkes Bay_185 Lawn road, Hasting_2019_Day_1 
##                                                     2 
##        Apple_Hawkes Bay_2080 Pakawhai Road_2019_Day_1 
##                                                     2 
## Apple_Hawkes Bay_241 Evenden Road, Twyford_2018_Day_1 
##                                                     3 
## Apple_Hawkes Bay_241 Evenden Road, Twyford_2019_Day_1 
##                                                     1 
##            Apple_Hawkes Bay_382 Twyford Rd_2018_Day_1 
##                                                     1
#sppr
#zeros are in there now, just because of the merge above they are out of order

####################################################
#impact of crop, region and flowering time
###################################################

#crop
sppr_aov <-aov(sppr ~ Crop, data = meta_data_day1)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7   9282  1325.9     100 <2e-16 ***
## Residuals   288   3819    13.3                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(sppr_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = sppr ~ Crop, data = meta_data_day1)
## 
## $Crop
##                            diff         lwr        upr     p adj
## Avocado-Apple        4.56864564   1.7152254  7.4220659 0.0000459
## Carrot-Apple        12.82535885  10.0346744 15.6160433 0.0000000
## Kiwifruit-Apple      1.46503497  -1.3627657  4.2928357 0.7608700
## Onion-Apple         14.65272727  11.8081623 17.4972922 0.0000000
## Pak Choi-Apple      12.65077605   9.7123623 15.5891898 0.0000000
## Pear-Apple           1.41558442  -2.3856254  5.2167942 0.9480974
## Radish-Apple         6.04545455   1.9396787 10.1512304 0.0002672
## Carrot-Avocado       8.25671321   6.0906885 10.4227380 0.0000000
## Kiwifruit-Avocado   -3.10361068  -5.3172503 -0.8899710 0.0006623
## Onion-Avocado       10.08408163   7.8490663 12.3190969 0.0000000
## Pak Choi-Avocado     8.08213041   5.7288307 10.4354301 0.0000000
## Pear-Avocado        -3.15306122  -6.5224736  0.2163511 0.0854379
## Radish-Avocado       1.47680891  -2.2327920  5.1864098 0.9268761
## Kiwifruit-Carrot   -11.36032389 -13.4924854 -9.2281624 0.0000000
## Onion-Carrot         1.82736842  -0.3269773  3.9817142 0.1638473
## Pak Choi-Carrot     -0.17458280  -2.4514077  2.1022421 0.9999980
## Pear-Carrot        -11.40977444 -14.7262261 -8.0933227 0.0000000
## Radish-Carrot       -6.77990431 -10.4414683 -3.1183403 0.0000010
## Onion-Kiwifruit     13.18769231  10.9854791 15.3899055 0.0000000
## Pak Choi-Kiwifruit  11.18574109   8.8635721 13.5079101 0.0000000
## Pear-Kiwifruit      -0.04945055  -3.3971944  3.2982933 1.0000000
## Radish-Kiwifruit     4.58041958   0.8904889  8.2703503 0.0044896
## Pak Choi-Onion      -2.00195122  -4.3445058  0.3406033 0.1567626
## Pear-Onion         -13.23714286 -16.5990593 -9.8752264 0.0000000
## Radish-Onion        -8.60727273 -12.3100665 -4.9044790 0.0000000
## Pear-Pak Choi      -11.23519164 -14.6768784 -7.7935048 0.0000000
## Radish-Pak Choi     -6.60532151 -10.3806900 -2.8299530 0.0000052
## Radish-Pear          4.62987013   0.1501014  9.1096388 0.0371083
#impact of region
sppr_aov <-aov(sppr ~ Region, data = meta_data_day1)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Region       11   8256   750.5      44 <2e-16 ***
## Residuals   284   4845    17.1                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(sppr_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = sppr ~ Region, data = meta_data_day1)
## 
## $Region
##                                  diff          lwr         upr     p adj
## Bay of Plenty-Auckland     -9.0000000 -16.08358054  -1.9164195 0.0021958
## Canterbury-Auckland        -0.5673077  -7.50263546   6.3680201 1.0000000
## Hawkes Bay-Auckland       -10.3833333 -17.62853726  -3.1381294 0.0002272
## Marlborough-Auckland        2.6309524  -4.79465769  10.0565624 0.9909283
## Northland-Auckland         -8.4375000 -16.04648608  -0.8285139 0.0158279
## Otago-Auckland              4.3611111  -3.81830315  12.5405254 0.8394588
## Poverty Bay-Auckland      -12.5000000 -20.83522663  -4.1647734 0.0000835
## Southland-Auckland          0.9166667  -7.26274759   9.0960809 0.9999999
## Tasman-Auckland           -11.2812500 -18.49976801  -4.0627320 0.0000311
## Wairarapa-Auckland          0.4722222  -7.70719204   8.6516365 1.0000000
## Whangarei-Auckland         -7.7500000 -16.53610032   1.0361003 0.1440736
## Canterbury-Bay of Plenty    8.4326923   6.05756833  10.8078163 0.0000000
## Hawkes Bay-Bay of Plenty   -1.3833333  -4.55120685   1.7845402 0.9547151
## Marlborough-Bay of Plenty  11.6309524   8.06975490  15.1921499 0.0000000
## Northland-Bay of Plenty     0.5625000  -3.36676351   4.4917635 0.9999986
## Otago-Bay of Plenty        13.3611111   8.41689628  18.3053259 0.0000000
## Poverty Bay-Bay of Plenty  -3.5000000  -8.69792705   1.6979270 0.5372219
## Southland-Bay of Plenty     9.9166667   4.97245184  14.8608815 0.0000000
## Tasman-Bay of Plenty       -2.2812500  -5.38760556   0.8251056 0.3959120
## Wairarapa-Bay of Plenty     9.4722222   4.52800739  14.4164371 0.0000001
## Whangarei-Bay of Plenty     1.2500000  -4.64389527   7.1438953 0.9999206
## Hawkes Bay-Canterbury      -9.8160256 -12.63685543  -6.9951959 0.0000000
## Marlborough-Canterbury      3.1982601  -0.05808536   6.4546055 0.0594537
## Northland-Canterbury       -7.8701923 -11.52543099  -4.2149536 0.0000000
## Otago-Canterbury            4.9284188   0.19905116   9.6577864 0.0326544
## Poverty Bay-Canterbury    -11.9326923 -16.92669908  -6.9386855 0.0000000
## Southland-Canterbury        1.4839744  -3.24539329   6.2133420 0.9967869
## Tasman-Canterbury         -10.7139423 -13.46550602  -7.9623786 0.0000000
## Wairarapa-Canterbury        1.0395299  -3.68983773   5.7688976 0.9998867
## Whangarei-Canterbury       -7.1826923 -12.89755534  -1.4678293 0.0026257
## Marlborough-Hawkes Bay     13.0142857   9.14156131  16.8870101 0.0000000
## Northland-Hawkes Bay        1.9458333  -2.26783235   6.1594990 0.9335068
## Otago-Hawkes Bay           14.7444444   9.57132865  19.9175602 0.0000000
## Poverty Bay-Hawkes Bay     -2.1166667  -7.53278265   3.2994493 0.9801023
## Southland-Hawkes Bay       11.3000000   6.12688420  16.4731158 0.0000000
## Tasman-Hawkes Bay          -0.8979167  -4.35700782   2.5611745 0.9994333
## Wairarapa-Hawkes Bay       10.8555556   5.68243976  16.0286714 0.0000000
## Whangarei-Hawkes Bay        2.6333333  -3.45385553   8.7205222 0.9576264
## Northland-Marlborough     -11.0684524 -15.58527465  -6.5516301 0.0000000
## Otago-Marlborough           1.7301587  -3.69274012   7.1530576 0.9962816
## Poverty Bay-Marlborough   -15.1309524 -20.78612853  -9.4757762 0.0000000
## Southland-Marlborough      -1.7142857  -7.13718456   3.7086131 0.9965699
## Tasman-Marlborough        -13.9122024 -17.73476912 -10.0896356 0.0000000
## Wairarapa-Marlborough      -2.1587302  -7.58162901   3.2641687 0.9770728
## Whangarei-Marlborough     -10.3809524 -16.68179146  -4.0801133 0.0000078
## Otago-Northland            12.7986111   7.12720774  18.4700145 0.0000000
## Poverty Bay-Northland      -4.0625000  -9.95639527   1.8313953 0.4993686
## Southland-Northland         9.3541667   3.68276330  15.0255700 0.0000076
## Tasman-Northland           -2.8437500  -7.01136331   1.3238633 0.5157890
## Wairarapa-Northland         8.9097222   3.23831885  14.5811256 0.0000273
## Whangarei-Northland         0.6875000  -5.82844639   7.2034464 0.9999999
## Poverty Bay-Otago         -16.8611111 -23.47504716 -10.2471751 0.0000000
## Southland-Otago            -3.4444444  -9.86090489   2.9720160 0.8333128
## Tasman-Otago              -15.6423611 -20.77803529 -10.5066869 0.0000000
## Wairarapa-Otago            -3.8888889 -10.30534934   2.5275716 0.6949111
## Whangarei-Otago           -12.1111111 -19.28493198  -4.9372902 0.0000040
## Southland-Poverty Bay      13.4166667   6.80273062  20.0306027 0.0000000
## Tasman-Poverty Bay          1.2187500  -4.16161565   6.5991157 0.9998477
## Wairarapa-Poverty Bay      12.9722222   6.35828618  19.5861583 0.0000000
## Whangarei-Poverty Bay       4.7500000  -2.60097893  12.1009789 0.6020319
## Tasman-Southland          -12.1979167 -17.33359085  -7.0622425 0.0000000
## Wairarapa-Southland        -0.4444444  -6.86090489   5.9720160 1.0000000
## Whangarei-Southland        -8.6666667 -15.84048754  -1.4928458 0.0048789
## Wairarapa-Tasman           11.7534722   6.61779804  16.8891464 0.0000000
## Whangarei-Tasman            3.5312500  -2.52415176   9.5866518 0.7446545
## Whangarei-Wairarapa        -8.2222222 -15.39604309  -1.0484014 0.0103143
#impact of crop and region
sppr_aov <-aov(sppr ~ Crop+Region, data = meta_data_day1)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7   9282  1325.9 101.363 <2e-16 ***
## Region       11    195    17.8   1.358  0.193    
## Residuals   277   3623    13.1                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(sppr_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = sppr ~ Crop + Region, data = meta_data_day1)
## 
## $Crop
##                            diff        lwr        upr     p adj
## Avocado-Apple        4.56864564   1.733688  7.4036033 0.0000401
## Carrot-Apple        12.82535885  10.052731 15.5979866 0.0000000
## Kiwifruit-Apple      1.46503497  -1.344469  4.2745388 0.7544528
## Onion-Apple         14.65272727  11.826568 17.4788869 0.0000000
## Pak Choi-Apple      12.65077605   9.731375 15.5701773 0.0000000
## Pear-Apple           1.41558442  -2.361030  5.1921991 0.9461956
## Radish-Apple         6.04545455   1.966244 10.1246647 0.0002373
## Carrot-Avocado       8.25671321   6.104703 10.4087231 0.0000000
## Kiwifruit-Avocado   -3.10361068  -5.302927 -0.9042940 0.0005938
## Onion-Avocado       10.08408163   7.863528 12.3046357 0.0000000
## Pak Choi-Avocado     8.08213041   5.744057 10.4202035 0.0000000
## Pear-Avocado        -3.15306122  -6.500672  0.1945499 0.0813428
## Radish-Avocado       1.47680891  -2.208790  5.1624075 0.9243173
## Kiwifruit-Carrot   -11.36032389 -13.478690 -9.2419582 0.0000000
## Onion-Carrot         1.82736842  -0.313038  3.9677749 0.1575968
## Pak Choi-Carrot     -0.17458280  -2.436676  2.0875103 0.9999979
## Pear-Carrot        -11.40977444 -14.704768 -8.1147813 0.0000000
## Radish-Carrot       -6.77990431 -10.417777 -3.1420318 0.0000009
## Onion-Kiwifruit     13.18769231  10.999728 15.3756565 0.0000000
## Pak Choi-Kiwifruit  11.18574109   8.878597 13.4928849 0.0000000
## Pear-Kiwifruit      -0.04945055  -3.375533  3.2766323 1.0000000
## Radish-Kiwifruit     4.58041958   0.914364  8.2464752 0.0041119
## Pak Choi-Onion      -2.00195122  -4.329349  0.3254462 0.1506732
## Pear-Onion         -13.23714286 -16.577307 -9.8969791 0.0000000
## Radish-Onion        -8.60727273 -12.286108 -4.9284373 0.0000000
## Pear-Pak Choi      -11.23519164 -14.654610 -7.8157737 0.0000000
## Radish-Pak Choi     -6.60532151 -10.356262 -2.8543809 0.0000044
## Radish-Pear          4.62987013   0.179087  9.0806533 0.0349090
## 
## $Region
##                                  diff         lwr        upr     p adj
## Bay of Plenty-Auckland     0.43996008  -5.7644226  6.6443428 1.0000000
## Canterbury-Auckland       -0.24638096  -6.3209117  5.8281498 1.0000000
## Hawkes Bay-Auckland        0.37411004  -5.9718357  6.7200558 1.0000000
## Marlborough-Auckland       0.62900116  -5.8749592  7.1329615 1.0000000
## Northland-Auckland        -0.35536959  -7.0199457  6.3092065 1.0000000
## Otago-Auckland             3.17132363  -3.9928804 10.3355277 0.9500725
## Poverty Bay-Auckland      -1.31425891  -8.6149363  5.9864184 0.9999848
## Southland-Auckland         0.91666667  -6.2475374  8.0808707 0.9999996
## Tasman-Auckland            0.28620313  -6.0363689  6.6087752 1.0000000
## Wairarapa-Auckland        -1.52972900  -8.6939331  5.6344751 0.9999147
## Whangarei-Auckland         0.33213041  -7.3634592  8.0277200 1.0000000
## Canterbury-Bay of Plenty  -0.68634105  -2.7666701  1.3939880 0.9950136
## Hawkes Bay-Bay of Plenty  -0.06585005  -2.8405343  2.7088343 1.0000000
## Marlborough-Bay of Plenty  0.18904108  -2.9301487  3.3082309 1.0000000
## Northland-Bay of Plenty   -0.79532967  -4.2369020  2.6462426 0.9998141
## Otago-Bay of Plenty        2.73136355  -1.5991866  7.0619137 0.6381495
## Poverty Bay-Bay of Plenty -1.75421900  -6.3069912  2.7985532 0.9821359
## Southland-Bay of Plenty    0.47670658  -3.8538436  4.8072568 0.9999999
## Tasman-Bay of Plenty      -0.15375696  -2.8745588  2.5670449 1.0000000
## Wairarapa-Bay of Plenty   -1.96968908  -6.3002393  2.3608611 0.9398406
## Whangarei-Bay of Plenty   -0.10782967  -5.2701881  5.0545288 1.0000000
## Hawkes Bay-Canterbury      0.62049100  -1.8502238  3.0912058 0.9995847
## Marlborough-Canterbury     0.87538212  -1.9767932  3.7275574 0.9973496
## Northland-Canterbury      -0.10898863  -3.3105474  3.0925702 1.0000000
## Otago-Canterbury           3.41770460  -0.7246647  7.5600739 0.2222689
## Poverty Bay-Canterbury    -1.06787795  -5.4420400  3.3062841 0.9996834
## Southland-Canterbury       1.16304763  -2.9793217  5.3054170 0.9988168
## Tasman-Canterbury          0.53258409  -1.8774618  2.9426300 0.9998801
## Wairarapa-Canterbury      -1.28334804  -5.4257174  2.8590213 0.9971195
## Whangarei-Canterbury       0.57851137  -4.4270359  5.5840587 0.9999999
## Marlborough-Hawkes Bay     0.25489113  -3.1371596  3.6469418 1.0000000
## Northland-Hawkes Bay      -0.72947962  -4.4201548  2.9611955 0.9999606
## Otago-Hawkes Bay           2.79721360  -1.7338269  7.3282541 0.6691088
## Poverty Bay-Hawkes Bay    -1.68836895  -6.4322490  3.0555111 0.9905593
## Southland-Hawkes Bay       0.54255663  -3.9884839  5.0735971 0.9999998
## Tasman-Hawkes Bay         -0.08790691  -3.1176636  2.9418498 1.0000000
## Wairarapa-Hawkes Bay      -1.90383903  -6.4348795  2.6272015 0.9655847
## Whangarei-Hawkes Bay      -0.04197962  -5.3736405  5.2896813 1.0000000
## Northland-Marlborough     -0.98437075  -4.9405754  2.9718339 0.9996200
## Otago-Marlborough          2.54232247  -2.2074986  7.2921435 0.8357475
## Poverty Bay-Marlborough   -1.94326007  -6.8965287  3.0100085 0.9794491
## Southland-Marlborough      0.28766551  -4.4621555  5.0374865 1.0000000
## Tasman-Marlborough        -0.34279804  -3.6909165  3.0053205 1.0000000
## Wairarapa-Marlborough     -2.15873016  -6.9085512  2.5910909 0.9401530
## Whangarei-Marlborough     -0.29687075  -5.8156641  5.2219226 1.0000000
## Otago-Northland            3.52669322  -1.4407885  8.4941750 0.4507462
## Poverty Bay-Northland     -0.95888932  -6.1212478  4.2034691 0.9999790
## Southland-Northland        1.27203625  -3.6954455  6.2395180 0.9995002
## Tasman-Northland           0.64157271  -3.0087660  4.2919114 0.9999881
## Wairarapa-Northland       -1.17435941  -6.1418412  3.7931223 0.9997680
## Whangarei-Northland        0.68750000  -5.0197020  6.3947020 0.9999998
## Poverty Bay-Otago         -4.48558255 -10.2786120  1.3074469 0.3124387
## Southland-Otago           -2.25465697  -7.8747210  3.3654071 0.9756174
## Tasman-Otago              -2.88512051  -7.3833665  1.6131255 0.6130594
## Wairarapa-Otago           -4.70105263 -10.3211167  0.9190114 0.2046399
## Whangarei-Otago           -2.83919322  -9.1226158  3.4442294 0.9424718
## Southland-Poverty Bay      2.23092558  -3.5621038  8.0239550 0.9822093
## Tasman-Poverty Bay         1.60046204  -3.1121049  6.3130290 0.9936163
## Wairarapa-Poverty Bay     -0.21547009  -6.0084995  5.5775593 1.0000000
## Whangarei-Poverty Bay      1.64638932  -4.7922029  8.0849816 0.9995069
## Tasman-Southland          -0.63046354  -5.1287096  3.8677825 0.9999989
## Wairarapa-Southland       -2.44639566  -8.0664597  3.1736684 0.9556211
## Whangarei-Southland       -0.58453625  -6.8679589  5.6988864 1.0000000
## Wairarapa-Tasman          -1.81593212  -6.3141782  2.6823139 0.9744347
## Whangarei-Tasman           0.04592729  -5.2578918  5.3497464 1.0000000
## Whangarei-Wairarapa        1.86185941  -4.4215632  8.1452820 0.9980658
#note region is no longer significant when we drop those other samples

#region within a crop
#I did this for beta and then decided I prefer it because of the biased sampling across crops/regions
#radish has only one location
categories_crop <- meta_data_day1 %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
#filtered_sppr <- sppr[grepl('^Apple', names(sppr))]
filtered_sppr <- sppr[grepl(paste0('^',item), names(sppr))]
filtered_meta_data_day1<-meta_data_day1 %>% filter(sample_ID %in% names(filtered_sppr))
sppr_aov <-aov(filtered_sppr ~ Region, data = filtered_meta_data_day1)
print(summary(sppr_aov))
print(TukeyHSD(sppr_aov))
}
## [1] "Analysing Apple"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1  0.274  0.2744   0.214  0.648
## Residuals   20 25.589  1.2795               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                        diff        lwr      upr     p adj
## Tasman-Hawkes Bay 0.2321429 -0.8135956 1.277881 0.6483192
## 
## [1] "Analysing Pear"
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Region       1  38.06   38.06   11.67 0.00512 **
## Residuals   12  39.15    3.26                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                    diff       lwr       upr     p adj
## Tasman-Hawkes Bay -3.65 -5.978248 -1.321752 0.0051188
## 
## [1] "Analysing Avocado"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       2    5.6   2.779   0.258  0.774
## Residuals   46  496.4  10.791               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                                diff       lwr      upr     p adj
## Northland-Bay of Plenty -0.72453704 -3.234532 1.785458 0.7652147
## Whangarei-Bay of Plenty -0.03703704 -3.627745 3.553671 0.9996560
## Whangarei-Northland      0.68750000 -3.121021 4.496021 0.9002447
## 
## [1] "Analysing Kiwifruit"
##             Df Sum Sq Mean Sq F value Pr(>F)   
## Region       3  48.69  16.229   4.797 0.0053 **
## Residuals   48 162.39   3.383                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                                 diff        lwr       upr     p adj
## Hawkes Bay-Bay of Plenty  -1.6507937 -3.6010554 0.2994681 0.1238499
## Poverty Bay-Bay of Plenty -1.8452381 -3.8790348 0.1885586 0.0878478
## Tasman-Bay of Plenty       0.6190476 -1.0699286 2.3080238 0.7639800
## Poverty Bay-Hawkes Bay    -0.1944444 -2.5730443 2.1841554 0.9963139
## Tasman-Hawkes Bay          2.2698413  0.1784186 4.3612640 0.0286208
## Tasman-Poverty Bay         2.4642857  0.2947565 4.6338149 0.0202320
## 
## [1] "Analysing Pakchoi"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3   45.1   15.03   0.907  0.447
## Residuals   37  613.3   16.58               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                             diff       lwr       upr     p adj
## Canterbury-Auckland   -0.5100000 -6.407272  5.387272 0.9954882
## Hawkes Bay-Auckland    3.2500000 -5.113934 11.613934 0.7242716
## Southland-Auckland     0.9166667 -5.664041  7.497375 0.9817868
## Hawkes Bay-Canterbury  3.7600000 -2.931147 10.451147 0.4410908
## Southland-Canterbury   1.4266667 -2.830301  5.683635 0.8041139
## Southland-Hawkes Bay  -2.3333333 -9.633973  4.967307 0.8253941
## 
## [1] "Analysing Onion"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3   34.2   11.41   0.462   0.71
## Residuals   46 1137.0   24.72               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                              diff       lwr      upr     p adj
## Marlborough-Canterbury  0.5142857 -3.965768 4.994340 0.9899256
## Otago-Canterbury        0.9333333 -5.910062 7.776729 0.9833602
## Wairarapa-Canterbury   -1.6444444 -7.232053 3.943165 0.8611163
## Otago-Marlborough       0.4190476 -6.175413 7.013509 0.9982465
## Wairarapa-Marlborough  -2.1587302 -7.438524 3.121064 0.6974441
## Wairarapa-Otago        -2.5777778 -9.969490 4.813934 0.7891899
## 
## [1] "Analysing Carrot"
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Region       1  139.6   139.6   8.024 0.00644 **
## Residuals   55  957.2    17.4                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_sppr ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                      diff      lwr      upr     p adj
## Otago-Canterbury 6.127358 1.792274 10.46244 0.0064408
#impact of flowering time
sppr_aov <-aov(sppr ~ flowertime, data = meta_data_day1)
summary(sppr_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## flowertime    1   8193    8193   490.8 <2e-16 ***
## Residuals   294   4908      17                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#TukeyHSD(sppr_aov)

#can plot that out
sppr_df <- sppr %>% 
  enframe() %>% 
  full_join(meta_data_day1, by = c("name" = "sample_ID"))

#sppr_df$Crop <- factor(sppr_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
sppr_df$Crop <- factor(sppr_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

#just crop
plot_sppr <- ggplot(sppr_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  scale_fill_manual(values=col_bound)+
  theme_bw()+
  labs(x = "Crop",
       y = "Number of species per sample",
       title = "Species richness")
plot_sppr

#other significant factor was flowertime
plot_sppr <- ggplot(sppr_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~flowertime, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Number of species per sample",
       title = "Species richness")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

Simpson day 1

#using simpson which is less sensitive to rare species which should be more robust to the sampling biases in the data
simpsondiv<-diversity(counts_day1_wide,index='simpson')
head(simpsondiv)
##        Apple_Hawkes Bay_104 Moteo-Pa road _2018_Day_1 
##                                            0.43555556 
##    Apple_Hawkes Bay_185 Lawn road, Hasting_2019_Day_1 
##                                            0.50000000 
##        Apple_Hawkes Bay_2080 Pakawhai Road_2019_Day_1 
##                                            0.04542996 
## Apple_Hawkes Bay_241 Evenden Road, Twyford_2018_Day_1 
##                                            0.44897959 
## Apple_Hawkes Bay_241 Evenden Road, Twyford_2019_Day_1 
##                                            0.00000000 
##            Apple_Hawkes Bay_382 Twyford Rd_2018_Day_1 
##                                            0.00000000
#simpsondiv
####################################################
#impact of crop, region and flowering time
###################################################

#crop
simpsondiv_aov <-aov(simpsondiv ~ Crop, data = meta_data_day1)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7  6.678   0.954   16.43 <2e-16 ***
## Residuals   288 16.717   0.058                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(simpsondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = simpsondiv ~ Crop, data = meta_data_day1)
## 
## $Crop
##                           diff           lwr           upr     p adj
## Avocado-Apple       0.10568504 -0.0831058977  0.2944759767 0.6812580
## Carrot-Apple        0.39081652  0.2061763819  0.5754566662 0.0000000
## Kiwifruit-Apple     0.02257369 -0.1645221801  0.2096695558 0.9999558
## Onion-Apple         0.37862659  0.1904215503  0.5668316343 0.0000001
## Pak Choi-Apple      0.19410328 -0.0003110861  0.3885176468 0.0507012
## Pear-Apple          0.22402144 -0.0274781303  0.4755210024 0.1208516
## Radish-Apple        0.13408860 -0.1375619821  0.4057391763 0.8032062
## Carrot-Avocado      0.28513148  0.1418207199  0.4284422492 0.0000001
## Kiwifruit-Avocado  -0.08311135 -0.2295724624  0.0633497591 0.6658494
## Onion-Avocado       0.27294155  0.1250661626  0.4208169429 0.0000012
## Pak Choi-Avocado    0.08841824 -0.0672832012  0.2441196828 0.6650263
## Pear-Avocado        0.11833640 -0.1045941421  0.3412669351 0.7374438
## Radish-Avocado      0.02840356 -0.2170348876  0.2738420027 0.9999668
## Kiwifruit-Carrot   -0.36824284 -0.5093131062 -0.2271725661 0.0000000
## Onion-Carrot       -0.01218993 -0.1547279798  0.1303481163 0.9999958
## Pak Choi-Carrot    -0.19671324 -0.3473548820 -0.0460716055 0.0021407
## Pear-Carrot        -0.16679509 -0.3862215886  0.0526314126 0.2858009
## Radish-Carrot      -0.25672793 -0.4989881063 -0.0144677477 0.0291358
## Onion-Kiwifruit     0.35605290  0.2103478021  0.5017580068 0.0000000
## Pak Choi-Kiwifruit  0.17152959  0.0178878505  0.3251713345 0.0168393
## Pear-Kiwifruit      0.20144775 -0.0200491387  0.4229446350 0.1049291
## Radish-Kiwifruit    0.11151491 -0.1326220937  0.3556519121 0.8592171
## Pak Choi-Onion     -0.18452331 -0.3395138244 -0.0295327996 0.0078271
## Pear-Onion         -0.15460516 -0.3770397420  0.0678294295 0.4030935
## Radish-Onion       -0.24453800 -0.4895260566  0.0004500662 0.0508057
## Pear-Pak Choi       0.02991816 -0.1977942791  0.2576305905 0.9999213
## Radish-Pak Choi    -0.06001468 -0.3098045114  0.1897751449 0.9959104
## Radish-Pear        -0.08993284 -0.3863279203  0.2064622424 0.9833045
#impact of region
simpsondiv_aov <-aov(simpsondiv ~ Region, data = meta_data_day1)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## Region       11  5.551  0.5046   8.031 3.42e-12 ***
## Residuals   284 17.844  0.0628                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(simpsondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = simpsondiv ~ Region, data = meta_data_day1)
## 
## $Region
##                                   diff          lwr         upr     p adj
## Bay of Plenty-Auckland     0.056939646 -0.372967210  0.48684650 0.9999994
## Canterbury-Auckland        0.336081164 -0.084828140  0.75699047 0.2673302
## Hawkes Bay-Auckland        0.081731377 -0.357984502  0.52144726 0.9999790
## Marlborough-Auckland       0.365304571 -0.085360267  0.81596941 0.2461872
## Northland-Auckland         0.170887793 -0.290906247  0.63268183 0.9870681
## Otago-Auckland             0.436486777 -0.059926900  0.93290045 0.1473536
## Poverty Bay-Auckland      -0.024253364 -0.530123389  0.48161666 1.0000000
## Southland-Auckland         0.139927882 -0.356485796  0.63634156 0.9987771
## Tasman-Auckland            0.108342157 -0.329754136  0.54643845 0.9996429
## Wairarapa-Auckland         0.363981971 -0.132431706  0.86039565 0.3984638
## Whangarei-Auckland         0.091202301 -0.442031526  0.62443613 0.9999910
## Canterbury-Bay of Plenty   0.279141518  0.134993784  0.42328925 0.0000000
## Hawkes Bay-Bay of Plenty   0.024791731 -0.167468460  0.21705192 0.9999995
## Marlborough-Bay of Plenty  0.308364925  0.092233661  0.52449619 0.0002496
## Northland-Bay of Plenty    0.113948147 -0.124521270  0.35241756 0.9168400
## Otago-Bay of Plenty        0.379547131  0.079479695  0.67961457 0.0023651
## Poverty Bay-Bay of Plenty -0.081193009 -0.396658396  0.23427238 0.9994773
## Southland-Bay of Plenty    0.082988236 -0.217079200  0.38305567 0.9989741
## Tasman-Bay of Plenty       0.051402511 -0.137124116  0.23992914 0.9991013
## Wairarapa-Bay of Plenty    0.307042326  0.006974889  0.60710976 0.0396349
## Whangarei-Bay of Plenty    0.034262655 -0.323441470  0.39196678 1.0000000
## Hawkes Bay-Canterbury     -0.254349787 -0.425547680 -0.08315189 0.0001033
## Marlborough-Canterbury     0.029223407 -0.168406198  0.22685301 0.9999980
## Northland-Canterbury      -0.165193371 -0.387032052  0.05664531 0.3735864
## Otago-Canterbury           0.100405614 -0.186622615  0.38743384 0.9918404
## Poverty Bay-Canterbury    -0.360334527 -0.663423867 -0.05724519 0.0061840
## Southland-Canterbury      -0.196153282 -0.483181510  0.09087495 0.5132980
## Tasman-Canterbury         -0.227739007 -0.394733099 -0.06074491 0.0006177
## Wairarapa-Canterbury       0.027900808 -0.259127421  0.31492904 1.0000000
## Whangarei-Canterbury      -0.244878863 -0.591717412  0.10195969 0.4600955
## Marlborough-Hawkes Bay     0.283573194  0.048535171  0.51861122 0.0049768
## Northland-Hawkes Bay       0.089156416 -0.166573543  0.34488638 0.9920662
## Otago-Hawkes Bay           0.354755401  0.040795824  0.66871498 0.0124516
## Poverty Bay-Hawkes Bay    -0.105984740 -0.434692147  0.22272267 0.9959236
## Southland-Hawkes Bay       0.058196505 -0.255763071  0.37215608 0.9999796
## Tasman-Hawkes Bay          0.026610780 -0.183323587  0.23654515 0.9999996
## Wairarapa-Hawkes Bay       0.282250595 -0.031708982  0.59621017 0.1253785
## Whangarei-Hawkes Bay       0.009470924 -0.359964308  0.37890616 1.0000000
## Northland-Marlborough     -0.194416778 -0.468545497  0.07971194 0.4527233
## Otago-Marlborough          0.071182206 -0.257936857  0.40030127 0.9999035
## Poverty Bay-Marlborough   -0.389557934 -0.732774049 -0.04634182 0.0117414
## Southland-Marlborough     -0.225376689 -0.554495752  0.10374237 0.5099896
## Tasman-Marlborough        -0.256962414 -0.488956338 -0.02496849 0.0160541
## Wairarapa-Marlborough     -0.001322599 -0.330441663  0.32779646 1.0000000
## Whangarei-Marlborough     -0.274102270 -0.656504065  0.10829953 0.4353429
## Otago-Northland            0.265598984 -0.078601971  0.60979994 0.3178899
## Poverty Bay-Northland     -0.195141157 -0.552845282  0.16256297 0.8180431
## Southland-Northland       -0.030959911 -0.375160867  0.31324104 1.0000000
## Tasman-Northland          -0.062545636 -0.315480649  0.19038938 0.9996432
## Wairarapa-Northland        0.193094178 -0.151106777  0.53729513 0.7893028
## Whangarei-Northland       -0.079685492 -0.475142282  0.31577130 0.9999524
## Poverty Bay-Otago         -0.460740141 -0.862143983 -0.05933630 0.0101131
## Southland-Otago           -0.296558895 -0.685977823  0.09286003 0.3381988
## Tasman-Otago              -0.328144620 -0.639831842 -0.01645740 0.0291871
## Wairarapa-Otago           -0.072504806 -0.461923733  0.31691412 0.9999786
## Whangarei-Otago           -0.345284476 -0.780668073  0.09009912 0.2771072
## Southland-Poverty Bay      0.164181245 -0.237222597  0.56558509 0.9718590
## Tasman-Poverty Bay         0.132595521 -0.193942177  0.45913322 0.9733469
## Wairarapa-Poverty Bay      0.388235335 -0.013168508  0.78963918 0.0685135
## Whangarei-Poverty Bay      0.115455665 -0.330679763  0.56159109 0.9994495
## Tasman-Southland          -0.031585725 -0.343272946  0.28010150 1.0000000
## Wairarapa-Southland        0.224054090 -0.165364838  0.61347302 0.7607984
## Whangarei-Southland       -0.048725581 -0.484109177  0.38665802 0.9999999
## Wairarapa-Tasman           0.255639814 -0.056047407  0.56732704 0.2302464
## Whangarei-Tasman          -0.017139856 -0.384645909  0.35036620 1.0000000
## Whangarei-Wairarapa       -0.272779670 -0.708163267  0.16260393 0.6482293
#impact of crop and region
simpsondiv_aov <-aov(simpsondiv ~ Crop+Region, data = meta_data_day1)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7  6.678  0.9540  16.261 <2e-16 ***
## Region       11  0.466  0.0424   0.722  0.717    
## Residuals   277 16.251  0.0587                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(simpsondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = simpsondiv ~ Crop + Region, data = meta_data_day1)
## 
## $Crop
##                           diff          lwr          upr     p adj
## Avocado-Apple       0.10568504 -0.084171770  0.295541849 0.6871918
## Carrot-Apple        0.39081652  0.205133944  0.576499104 0.0000000
## Kiwifruit-Apple     0.02257369 -0.165578483  0.210725858 0.9999574
## Onion-Apple         0.37862659  0.189358986  0.567894199 0.0000001
## Pak Choi-Apple      0.19410328 -0.001408707  0.389615268 0.0532212
## Pear-Apple          0.22402144 -0.028898042  0.476940914 0.1253339
## Radish-Apple        0.13408860 -0.139095662  0.407272856 0.8074919
## Carrot-Avocado      0.28513148  0.141011618  0.429251351 0.0000001
## Kiwifruit-Avocado  -0.08311135 -0.230399350  0.064176647 0.6719470
## Onion-Avocado       0.27294155  0.124231290  0.421651815 0.0000014
## Pak Choi-Avocado    0.08841824 -0.068162258  0.244998739 0.6711324
## Pear-Avocado        0.11833640 -0.105852759  0.342525552 0.7426969
## Radish-Avocado      0.02840356 -0.218420580  0.275227695 0.9999679
## Kiwifruit-Carrot   -0.36824284 -0.510109558 -0.226376114 0.0000000
## Onion-Carrot       -0.01218993 -0.155532719  0.131152855 0.9999960
## Pak Choi-Carrot    -0.19671324 -0.348205372 -0.045221116 0.0023418
## Pear-Carrot        -0.16679509 -0.387460423  0.053870247 0.2926213
## Radish-Carrot      -0.25672793 -0.500355855 -0.013099999 0.0308126
## Onion-Kiwifruit     0.35605290  0.209525183  0.502580626 0.0000000
## Pak Choi-Kiwifruit  0.17152959  0.017020423  0.326038762 0.0179379
## Pear-Kiwifruit      0.20144775 -0.021299662  0.424195158 0.1090399
## Radish-Kiwifruit    0.11151491 -0.134000438  0.357030257 0.8625246
## Pak Choi-Onion     -0.18452331 -0.340388867 -0.028657757 0.0084212
## Pear-Onion         -0.15460516 -0.378295559  0.069085247 0.4104300
## Radish-Onion       -0.24453800 -0.490909206  0.001833215 0.0533294
## Pear-Pak Choi       0.02991816 -0.199079894  0.258916205 0.9999240
## Radish-Pak Choi    -0.06001468 -0.311214770  0.191185404 0.9960407
## Radish-Pear        -0.08993284 -0.388001302  0.208135625 0.9838026
## 
## $Region
##                                    diff        lwr       upr     p adj
## Bay of Plenty-Auckland     0.1817191030 -0.2337877 0.5972259 0.9541267
## Canterbury-Auckland        0.2155668359 -0.1912438 0.6223775 0.8446837
## Hawkes Bay-Auckland        0.2197826978 -0.2052046 0.6447700 0.8648359
## Marlborough-Auckland       0.1807812588 -0.2547883 0.6163508 0.9685545
## Northland-Auckland         0.2593060338 -0.1870199 0.7056320 0.7489212
## Otago-Auckland             0.2465457178 -0.2332403 0.7263317 0.8697686
## Poverty Bay-Auckland       0.1472762289 -0.3416493 0.6362018 0.9977576
## Southland-Auckland         0.1399278817 -0.3398581 0.6197139 0.9983247
## Tasman-Auckland            0.2225627502 -0.2008592 0.6459847 0.8516085
## Wairarapa-Auckland         0.1794586595 -0.3003273 0.6592446 0.9858712
## Whangarei-Auckland         0.1796205418 -0.3357523 0.6949933 0.9920646
## Canterbury-Bay of Plenty   0.0338477329 -0.1054717 0.1731671 0.9996979
## Hawkes Bay-Bay of Plenty   0.0380635947 -0.1477567 0.2238839 0.9999436
## Marlborough-Bay of Plenty -0.0009378442 -0.2098296 0.2079540 1.0000000
## Northland-Bay of Plenty    0.0775869308 -0.1528948 0.3080687 0.9940771
## Otago-Bay of Plenty        0.0648266147 -0.2251899 0.3548431 0.9998658
## Poverty Bay-Bay of Plenty -0.0344428741 -0.3393415 0.2704558 0.9999999
## Southland-Bay of Plenty   -0.0417912213 -0.3318077 0.2482253 0.9999985
## Tasman-Bay of Plenty       0.0408436471 -0.1413682 0.2230554 0.9998621
## Wairarapa-Bay of Plenty   -0.0022604435 -0.2922769 0.2877560 1.0000000
## Whangarei-Bay of Plenty   -0.0020985612 -0.3478211 0.3436240 1.0000000
## Hawkes Bay-Canterbury      0.0042158618 -0.1612476 0.1696794 1.0000000
## Marlborough-Canterbury    -0.0347855771 -0.2257954 0.1562243 0.9999829
## Northland-Canterbury       0.0437391979 -0.1706688 0.2581472 0.9999459
## Otago-Canterbury           0.0309788818 -0.2464351 0.3083929 0.9999999
## Poverty Bay-Canterbury    -0.0682906070 -0.3612278 0.2246466 0.9997975
## Southland-Canterbury      -0.0756389543 -0.3530530 0.2017751 0.9990981
## Tasman-Canterbury          0.0069959142 -0.1544046 0.1683964 1.0000000
## Wairarapa-Canterbury      -0.0361081764 -0.3135222 0.2413058 0.9999995
## Whangarei-Canterbury      -0.0359462941 -0.3711673 0.2992747 0.9999999
## Marlborough-Hawkes Bay    -0.0390014390 -0.2661667 0.1881638 0.9999906
## Northland-Hawkes Bay       0.0395233361 -0.2076408 0.2866874 0.9999955
## Otago-Hawkes Bay           0.0267630200 -0.2766803 0.3302063 1.0000000
## Poverty Bay-Hawkes Bay    -0.0725064688 -0.3902036 0.2451907 0.9998354
## Southland-Hawkes Bay      -0.0798548161 -0.3832981 0.2235885 0.9993528
## Tasman-Hawkes Bay          0.0027800524 -0.2001224 0.2056825 1.0000000
## Wairarapa-Hawkes Bay      -0.0403240383 -0.3437673 0.2631193 0.9999994
## Whangarei-Hawkes Bay      -0.0401621559 -0.3972229 0.3168986 0.9999999
## Northland-Marlborough      0.0785247750 -0.1864218 0.3434714 0.9980619
## Otago-Marlborough          0.0657644590 -0.2523305 0.3838595 0.9999382
## Poverty Bay-Marlborough   -0.0335050298 -0.3652249 0.2982148 1.0000000
## Southland-Marlborough     -0.0408533771 -0.3589484 0.2772416 0.9999996
## Tasman-Marlborough         0.0417814914 -0.1824416 0.2660046 0.9999783
## Wairarapa-Marlborough     -0.0013225993 -0.3194176 0.3167724 1.0000000
## Whangarei-Marlborough     -0.0011607170 -0.3707537 0.3684323 1.0000000
## Otago-Northland           -0.0127603161 -0.3454320 0.3199114 1.0000000
## Poverty Bay-Northland     -0.1120298049 -0.4577524 0.2336928 0.9957298
## Southland-Northland       -0.1193781521 -0.4520499 0.2132936 0.9898984
## Tasman-Northland          -0.0367432837 -0.2812061 0.2077195 0.9999977
## Wairarapa-Northland       -0.0798473743 -0.4125191 0.2528243 0.9997313
## Whangarei-Northland       -0.0796854920 -0.4618962 0.3025252 0.9999328
## Poverty Bay-Otago         -0.0992694888 -0.4872280 0.2886891 0.9995038
## Southland-Otago           -0.1066178361 -0.4829929 0.2697572 0.9987160
## Tasman-Otago              -0.0239829676 -0.3252300 0.2772641 1.0000000
## Wairarapa-Otago           -0.0670870583 -0.4434621 0.3092880 0.9999862
## Whangarei-Otago           -0.0669251759 -0.4877253 0.3538750 0.9999958
## Southland-Poverty Bay     -0.0073483473 -0.3953069 0.3806102 1.0000000
## Tasman-Poverty Bay         0.0752865212 -0.2403136 0.3908866 0.9997468
## Wairarapa-Poverty Bay      0.0321824305 -0.3557761 0.4201410 1.0000000
## Whangarei-Poverty Bay      0.0323443129 -0.3988475 0.4635361 1.0000000
## Tasman-Southland           0.0826348685 -0.2186122 0.3838819 0.9990460
## Wairarapa-Southland        0.0395307778 -0.3368443 0.4159059 0.9999999
## Whangarei-Southland        0.0396926601 -0.3811075 0.4604928 1.0000000
## Wairarapa-Tasman          -0.0431040907 -0.3443511 0.2581430 0.9999986
## Whangarei-Tasman          -0.0429422084 -0.3981384 0.3122540 0.9999998
## Whangarei-Wairarapa        0.0001618823 -0.4206382 0.4209620 1.0000000
#region within a crop
#I did this for beta and then decided I prefer it because of the biased sampling across crops/regions
#radish has only one location
categories_crop <- meta_data_day1 %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
#filtered_sppr <- sppr[grepl('^Apple', names(sppr))]
filtered_simpsondiv <- simpsondiv[grepl(paste0('^',item), names(simpsondiv))]
filtered_meta_data_day1<-meta_data_day1 %>% filter(sample_ID %in% names(filtered_simpsondiv))
simpsondiv_aov <-aov(filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
print(summary(simpsondiv_aov))
print(TukeyHSD(simpsondiv_aov))
}
## [1] "Analysing Apple"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1  0.085 0.08509   0.456  0.507
## Residuals   20  3.729 0.18643               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                         diff        lwr      upr     p adj
## Tasman-Hawkes Bay -0.1292832 -0.5284604 0.269894 0.5070381
## 
## [1] "Analysing Pear"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1 0.0166 0.01664   0.138  0.716
## Residuals   12 1.4441 0.12034               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                         diff        lwr       upr     p adj
## Tasman-Hawkes Bay 0.07632419 -0.3708278 0.5234762 0.7164509
## 
## [1] "Analysing Avocado"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       2 0.0748 0.03742   0.797  0.457
## Residuals   46 2.1590 0.04694               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                                 diff         lwr       upr     p adj
## Northland-Bay of Plenty  0.084087300 -0.08144638 0.2496210 0.4417108
## Whangarei-Bay of Plenty  0.004401808 -0.23240462 0.2412082 0.9988833
## Whangarei-Northland     -0.079685492 -0.33085664 0.1714857 0.7241302
## 
## [1] "Analysing Kiwifruit"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3  0.121 0.04044   0.335    0.8
## Residuals   48  5.791 0.12065               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                                  diff        lwr       upr     p adj
## Hawkes Bay-Bay of Plenty  -0.02417588 -0.3924753 0.3441236 0.9980787
## Poverty Bay-Bay of Plenty -0.04280049 -0.4268752 0.3412742 0.9908111
## Tasman-Bay of Plenty       0.08879767 -0.2301590 0.4077544 0.8800454
## Poverty Bay-Hawkes Bay    -0.01862461 -0.4678141 0.4305649 0.9995123
## Tasman-Hawkes Bay          0.11297355 -0.2819836 0.5079307 0.8713803
## Tasman-Poverty Bay         0.13159816 -0.2781091 0.5413054 0.8278400
## 
## [1] "Analysing Pakchoi"
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Region       3 0.2918 0.09728   2.726 0.0579 .
## Residuals   37 1.3204 0.03569                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                              diff         lwr        upr     p adj
## Canterbury-Auckland    0.24280198 -0.03083279 0.51643674 0.0973993
## Hawkes Bay-Auckland    0.33413560 -0.05395281 0.72222400 0.1127644
## Southland-Auckland     0.13992788 -0.16541844 0.44527421 0.6105373
## Hawkes Bay-Canterbury  0.09133362 -0.21913710 0.40180435 0.8579440
## Southland-Canterbury  -0.10287409 -0.30039837 0.09465018 0.5068087
## Southland-Hawkes Bay  -0.19420772 -0.53295905 0.14454362 0.4235200
## 
## [1] "Analysing Onion"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3 0.0411 0.01370   0.847  0.475
## Residuals   46 0.7443 0.01618               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                                diff         lwr        upr     p adj
## Marlborough-Canterbury -0.050901985 -0.16552540 0.06372143 0.6400772
## Otago-Canterbury        0.023108232 -0.15198192 0.19819838 0.9848712
## Wairarapa-Canterbury   -0.052224584 -0.19518509 0.09073593 0.7649682
## Otago-Marlborough       0.074010217 -0.09471087 0.24273131 0.6490195
## Wairarapa-Marlborough  -0.001322599 -0.13640758 0.13376239 0.9999935
## Wairarapa-Otago        -0.075332816 -0.26445179 0.11378616 0.7141963
## 
## [1] "Analysing Carrot"
##             Df Sum Sq  Mean Sq F value Pr(>F)
## Region       1 0.0047 0.004691   0.506   0.48
## Residuals   55 0.5098 0.009269               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_simpsondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                        diff         lwr       upr     p adj
## Otago-Canterbury 0.03551607 -0.06452693 0.1355591 0.4798109
#impact of flowering time
simpsondiv_aov <-aov(simpsondiv ~ flowertime, data = meta_data_day1)
summary(simpsondiv_aov)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## flowertime    1  4.571   4.571    71.4 1.37e-15 ***
## Residuals   294 18.824   0.064                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#TukeyHSD(simpsondiv_aov)

#can plot that out
simpsondiv_df <- simpsondiv %>% 
  enframe() %>% 
  full_join(meta_data_day1, by = c("name" = "sample_ID"))

#simpsondiv_df$Crop <- factor(simpsondiv_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
simpsondiv_df$Crop <- factor(simpsondiv_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

#just crop
plot_sppr <- ggplot(simpsondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  scale_fill_manual(values=col_bound)+
  theme_bw()+
  labs(x = "Crop",
       y = "Simpson",
       title = "Species richness: simpson")
plot_sppr

#other significant factor was flowertime
plot_sppr <- ggplot(simpsondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~flowertime, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Simpson",
       title = "Species richness: simpson")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

Shannon day 1

#########
#shannon#
#########

#hmmmm I actually dont like that, when say apple has a bunch of 1's its getting a very high score as its perfectly even

#having a look at shannon
shannondiv<-diversity(counts_day1_wide)
#head(shannondiv)
#crop
shannondiv_aov <-aov(shannondiv ~ Crop, data = meta_data_day1)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7 115.38  16.483   71.47 <2e-16 ***
## Residuals   288  66.42   0.231                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(shannondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = shannondiv ~ Crop, data = meta_data_day1)
## 
## $Crop
##                           diff         lwr         upr     p adj
## Avocado-Apple       0.86043039  0.48410372  1.23675707 0.0000000
## Carrot-Apple        1.79680237  1.42874969  2.16485506 0.0000000
## Kiwifruit-Apple     0.40254673  0.02959892  0.77549454 0.0241881
## Onion-Apple         1.82081378  1.44565499  2.19597256 0.0000000
## Pak Choi-Apple      1.21163937  0.82410322  1.59917552 0.0000000
## Pear-Apple          0.43453680 -0.06679021  0.93586382 0.1438746
## Radish-Apple        0.92837784  0.38688278  1.46987290 0.0000088
## Carrot-Avocado      0.93637198  0.65070327  1.22204069 0.0000000
## Kiwifruit-Avocado  -0.45788366 -0.74983212 -0.16593520 0.0000727
## Onion-Avocado       0.96038338  0.66561577  1.25515100 0.0000000
## Pak Choi-Avocado    0.35120898  0.04084129  0.66157666 0.0144610
## Pear-Avocado       -0.42589359 -0.87027249  0.01848531 0.0711344
## Radish-Avocado      0.06794745 -0.42129762  0.55719251 0.9998855
## Kiwifruit-Carrot   -1.39425564 -1.67545826 -1.11305302 0.0000000
## Onion-Carrot        0.02401140 -0.26011701  0.30813982 0.9999962
## Pak Choi-Carrot    -0.58516300 -0.88544472 -0.28488128 0.0000002
## Pear-Carrot        -1.36226557 -1.79965969 -0.92487145 0.0000000
## Radish-Carrot      -0.86842453 -1.35133420 -0.38551487 0.0000024
## Onion-Kiwifruit     1.41826705  1.12782558  1.70870852 0.0000000
## Pak Choi-Kiwifruit  0.80909264  0.50283066  1.11535462 0.0000000
## Pear-Kiwifruit      0.03199007 -0.40953106  0.47351120 0.9999987
## Radish-Kiwifruit    0.52583111  0.03918027  1.01248195 0.0239241
## Pak Choi-Onion     -0.60917441 -0.91812496 -0.30022385 0.0000001
## Pear-Onion         -1.38627697 -1.82966727 -0.94288668 0.0000000
## Radish-Onion       -0.89243594 -1.38078323 -0.40408864 0.0000015
## Pear-Pak Choi      -0.77710257 -1.23101347 -0.32319167 0.0000091
## Radish-Pak Choi    -0.28326153 -0.78118043  0.21465737 0.6629593
## Radish-Pear         0.49384104 -0.09697851  1.08466059 0.1782465
tukey_test<-TukeyHSD(shannondiv_aov)
tukey_test<-tukey_test$Crop
write.csv(tukey_test,'Alpha_shannon_diversity_onerep_countsvscrops.csv',quote = F,row.names = T)

#impact of region
shannondiv_aov <-aov(shannondiv ~ Region, data = meta_data_day1)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Region       11 101.04   9.186    32.3 <2e-16 ***
## Residuals   284  80.77   0.284                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(shannondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = shannondiv ~ Region, data = meta_data_day1)
## 
## $Region
##                                  diff          lwr          upr     p adj
## Bay of Plenty-Auckland    -0.05618322 -0.970801247  0.858434805 1.0000000
## Canterbury-Auckland        0.86109641 -0.034379508  1.756572330 0.0721780
## Hawkes Bay-Auckland       -0.43842117 -1.373907694  0.497065349 0.9266825
## Marlborough-Auckland       1.05937226  0.100592050  2.018152466 0.0165502
## Northland-Auckland         0.20463263 -0.777824726  1.187089992 0.9999336
## Otago-Auckland             1.30348739  0.247377467  2.359597321 0.0035112
## Poverty Bay-Auckland      -0.73413021 -1.810358323  0.342097906 0.5162938
## Southland-Auckland         0.22538493 -0.830724998  1.281494857 0.9999155
## Tasman-Auckland           -0.32818015 -1.260221041  0.603860734 0.9913840
## Wairarapa-Auckland         0.93032521 -0.125784717  1.986435137 0.1454482
## Whangarei-Auckland         0.07591753 -1.058526509  1.210361573 1.0000000
## Canterbury-Bay of Plenty   0.91727963  0.610608282  1.223950981 0.0000000
## Hawkes Bay-Bay of Plenty  -0.38223795 -0.791267568  0.026791664 0.0923402
## Marlborough-Bay of Plenty  1.11555548  0.655740644  1.575370313 0.0000000
## Northland-Bay of Plenty    0.26081585 -0.246522945  0.768154652 0.8696461
## Otago-Bay of Plenty        1.35967061  0.721283293  1.998057936 0.0000000
## Poverty Bay-Bay of Plenty -0.67794699 -1.349093134 -0.006800842 0.0452256
## Southland-Bay of Plenty    0.28156815 -0.356819172  0.919955472 0.9514210
## Tasman-Bay of Plenty      -0.27199693 -0.673083470  0.129089604 0.5257678
## Wairarapa-Bay of Plenty    0.98650843  0.348121108  1.624895752 0.0000410
## Whangarei-Bay of Plenty    0.13210075 -0.628907445  0.893108951 0.9999896
## Hawkes Bay-Canterbury     -1.29951758 -1.663737593 -0.935297574 0.0000000
## Marlborough-Canterbury     0.19827585 -0.222177088  0.618728782 0.9236394
## Northland-Canterbury      -0.65646378 -1.128421027 -0.184506529 0.0004197
## Otago-Canterbury           0.44239098 -0.168255692  1.053037658 0.4179729
## Poverty Bay-Canterbury    -1.59522662 -2.240042979 -0.950410260 0.0000000
## Southland-Canterbury      -0.63571148 -1.246358156 -0.025064807 0.0330102
## Tasman-Canterbury         -1.18927656 -1.544553075 -0.834000054 0.0000000
## Wairarapa-Canterbury       0.06922880 -0.541417876  0.679875473 0.9999999
## Whangarei-Canterbury      -0.78517888 -1.523070785 -0.047286972 0.0258903
## Marlborough-Hawkes Bay     1.49779343  0.997754851  1.997832009 0.0000000
## Northland-Hawkes Bay       0.64305381  0.098993556  1.187114055 0.0067278
## Otago-Hawkes Bay           1.74190857  1.073966001  2.409851132 0.0000000
## Poverty Bay-Hawkes Bay    -0.29570904 -0.995027310  0.403609237 0.9640362
## Southland-Hawkes Bay       0.66380610 -0.004136464  1.331748668 0.0531253
## Tasman-Hawkes Bay          0.11024102 -0.336390047  0.556872084 0.9996494
## Wairarapa-Hawkes Bay       1.36874638  0.700803816  2.036688948 0.0000000
## Whangarei-Hawkes Bay       0.51433870 -0.271627182  1.300304592 0.5824478
## Northland-Marlborough     -0.85473963 -1.437942857 -0.271536393 0.0001401
## Otago-Marlborough          0.24411514 -0.456078927  0.944309200 0.9920654
## Poverty Bay-Marlborough   -1.79350247 -2.523687719 -1.063317214 0.0000000
## Southland-Marlborough     -0.83398733 -1.534181392 -0.133793265 0.0060192
## Tasman-Marlborough        -1.38755241 -1.881114732 -0.893990091 0.0000000
## Wairarapa-Marlborough     -0.12904705 -0.829241112  0.571147015 0.9999807
## Whangarei-Marlborough     -0.98345473 -1.797006710 -0.169902741 0.0048339
## Otago-Northland            1.09885476  0.366574281  1.831135241 0.0000823
## Poverty Bay-Northland     -0.93876284 -1.699771039 -0.177754644 0.0035407
## Southland-Northland        0.02075230 -0.711528183  0.753032776 1.0000000
## Tasman-Northland          -0.53281279 -1.070926844  0.005301271 0.0550488
## Wairarapa-Northland        0.72569258 -0.006587903  1.457973057 0.0545940
## Whangarei-Northland       -0.12871510 -0.970041319  0.712611118 0.9999972
## Poverty Bay-Otago         -2.03761760 -2.891596053 -1.183639153 0.0000000
## Southland-Otago           -1.07810246 -1.906583253 -0.249621676 0.0014592
## Tasman-Otago              -1.63166755 -2.294775725 -0.968559371 0.0000000
## Wairarapa-Otago           -0.37316218 -1.201642973  0.455318604 0.9438252
## Whangarei-Otago           -1.22756986 -2.153839543 -0.301300181 0.0010538
## Southland-Poverty Bay      0.95951514  0.105536688  1.813493588 0.0133949
## Tasman-Poverty Bay         0.40595005 -0.288752206  1.100652316 0.7421455
## Wairarapa-Poverty Bay      1.66445542  0.810476968  2.518433869 0.0000000
## Whangarei-Poverty Bay      0.81004774 -0.139096241  1.759191722 0.1803585
## Tasman-Southland          -0.55356508 -1.216673261  0.109543094 0.2072837
## Wairarapa-Southland        0.70494028 -0.123540509  1.533421069 0.1839101
## Whangarei-Southland       -0.14946740 -1.075737078  0.776802283 0.9999951
## Wairarapa-Tasman           1.25850536  0.595397186  1.921613541 0.0000001
## Whangarei-Tasman           0.40409769 -0.377763912  1.185959284 0.8655156
## Whangarei-Wairarapa       -0.85440768 -1.780677358  0.071862003 0.1026926
#impact of crop and region
shannondiv_aov <-aov(shannondiv ~ Crop+Region, data = meta_data_day1)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Crop          7 115.38  16.483   74.58 <2e-16 ***
## Region       11   5.20   0.473    2.14  0.018 *  
## Residuals   277  61.22   0.221                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(shannondiv_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = shannondiv ~ Crop + Region, data = meta_data_day1)
## 
## $Crop
##                           diff         lwr          upr     p adj
## Avocado-Apple       0.86043039  0.49192752  1.228933268 0.0000000
## Carrot-Apple        1.79680237  1.43640148  2.157203270 0.0000000
## Kiwifruit-Apple     0.40254673  0.03735248  0.767740982 0.0193847
## Onion-Apple         1.82081378  1.45345452  2.188173037 0.0000000
## Pak Choi-Apple      1.21163937  0.83216007  1.591118671 0.0000000
## Pear-Apple          0.43453680 -0.05636766  0.925441263 0.1258570
## Radish-Apple        0.92837784  0.39814042  1.458615258 0.0000051
## Carrot-Avocado      0.93637198  0.65664230  1.216101660 0.0000000
## Kiwifruit-Avocado  -0.45788366 -0.74376254 -0.172004791 0.0000461
## Onion-Avocado       0.96038338  0.67174396  1.249022801 0.0000000
## Pak Choi-Avocado    0.35120898  0.04729381  0.655124141 0.0113242
## Pear-Avocado       -0.42589359 -0.86103389  0.009246704 0.0600071
## Radish-Avocado      0.06794745 -0.41112625  0.547021144 0.9998678
## Kiwifruit-Carrot   -1.39425564 -1.66961208 -1.118899205 0.0000000
## Onion-Carrot        0.02401140 -0.25421000  0.302232812 0.9999955
## Pak Choi-Carrot    -0.58516300 -0.87920189 -0.291124118 0.0000001
## Pear-Carrot        -1.36226557 -1.79056630 -0.933964841 0.0000000
## Radish-Carrot      -0.86842453 -1.34129454 -0.395554524 0.0000014
## Onion-Kiwifruit     1.41826705  1.13386383  1.702670259 0.0000000
## Pak Choi-Kiwifruit  0.80909264  0.50919782  1.108987456 0.0000000
## Pear-Kiwifruit      0.03199007 -0.40035186  0.464332009 0.9999985
## Radish-Kiwifruit    0.52583111  0.04929771  1.002364509 0.0191635
## Pak Choi-Onion     -0.60917441 -0.91170190 -0.306646912 0.0000001
## Pear-Onion         -1.38627697 -1.82044921 -0.952104734 0.0000000
## Radish-Onion       -0.89243594 -1.37063053 -0.414241348 0.0000009
## Pear-Pak Choi      -0.77710257 -1.22157669 -0.332628442 0.0000053
## Radish-Pak Choi    -0.28326153 -0.77082874  0.204305675 0.6379397
## Radish-Pear         0.49384104 -0.08469542  1.072377491 0.1577551
## 
## $Region
##                                   diff        lwr       upr     p adj
## Bay of Plenty-Auckland     0.495349858 -0.3111288 1.3018285 0.6762565
## Canterbury-Auckland        0.504987003 -0.2846128 1.2945868 0.6174062
## Hawkes Bay-Auckland        0.473352001 -0.3515278 1.2982318 0.7635964
## Marlborough-Auckland       0.450197851 -0.3952215 1.2956172 0.8403438
## Northland-Auckland         0.555841609 -0.3104554 1.4221386 0.6124883
## Otago-Auckland             0.704984722 -0.2262566 1.6362261 0.3472492
## Poverty Bay-Auckland       0.074962431 -0.8740184 1.0239433 1.0000000
## Southland-Auckland         0.225384930 -0.7058564 1.1566263 0.9997088
## Tasman-Auckland            0.571552271 -0.2502893 1.3933938 0.4845146
## Wairarapa-Auckland         0.321150803 -0.6100905 1.2523921 0.9927358
## Whangarei-Auckland         0.427126508 -0.5731871 1.4274402 0.9613235
## Canterbury-Bay of Plenty   0.009637145 -0.2607751 0.2800494 1.0000000
## Hawkes Bay-Bay of Plenty  -0.021997857 -0.3826661 0.3386704 1.0000000
## Marlborough-Bay of Plenty -0.045152007 -0.4506009 0.3602969 0.9999999
## Northland-Bay of Plenty    0.060491751 -0.3868621 0.5078456 0.9999992
## Otago-Bay of Plenty        0.209634864 -0.3532731 0.7725428 0.9863545
## Poverty Bay-Bay of Plenty -0.420387427 -1.0121810 0.1714061 0.4498113
## Southland-Bay of Plenty   -0.269964929 -0.8328729 0.2929430 0.9147033
## Tasman-Bay of Plenty       0.076202413 -0.2774619 0.4298667 0.9999067
## Wairarapa-Bay of Plenty   -0.174199056 -0.7371070 0.3887089 0.9971480
## Whangarei-Bay of Plenty   -0.068223350 -0.7392542 0.6028074 1.0000000
## Hawkes Bay-Canterbury     -0.031635002 -0.3527917 0.2895216 1.0000000
## Marlborough-Canterbury    -0.054789152 -0.4255301 0.3159518 0.9999980
## Northland-Canterbury       0.050854606 -0.3653010 0.4670102 0.9999997
## Otago-Canterbury           0.199997719 -0.3384495 0.7384449 0.9866396
## Poverty Bay-Canterbury    -0.430024572 -0.9986014 0.1385523 0.3487423
## Southland-Canterbury      -0.279602073 -0.8180493 0.2588451 0.8615896
## Tasman-Canterbury          0.066565268 -0.2467053 0.3798358 0.9999187
## Wairarapa-Canterbury      -0.183836200 -0.7222834 0.3546110 0.9933241
## Whangarei-Canterbury      -0.077860495 -0.7285081 0.5727872 0.9999998
## Marlborough-Hawkes Bay    -0.023154150 -0.4640709 0.4177626 1.0000000
## Northland-Hawkes Bay       0.082489608 -0.3972440 0.5622232 0.9999905
## Otago-Hawkes Bay           0.231632721 -0.3573360 0.8206015 0.9790554
## Poverty Bay-Hawkes Bay    -0.398389570 -1.0150243 0.2182452 0.6019657
## Southland-Hawkes Bay      -0.247967072 -0.8369358 0.3410017 0.9650781
## Tasman-Hawkes Bay          0.098200270 -0.2956236 0.4920241 0.9996121
## Wairarapa-Hawkes Bay      -0.152201199 -0.7411699 0.4367675 0.9994550
## Whangarei-Hawkes Bay      -0.046225493 -0.7392631 0.6468121 1.0000000
## Northland-Marlborough      0.105643758 -0.4086048 0.6198923 0.9999420
## Otago-Marlborough          0.254786871 -0.3626201 0.8721939 0.9698610
## Poverty Bay-Marlborough   -0.375235420 -1.0190876 0.2686168 0.7451227
## Southland-Marlborough     -0.224812921 -0.8422199 0.3925941 0.9886201
## Tasman-Marlborough         0.121354420 -0.3138518 0.5565607 0.9988895
## Wairarapa-Marlborough     -0.129047048 -0.7464540 0.4883599 0.9999310
## Whangarei-Marlborough     -0.023071343 -0.7404334 0.6942908 1.0000000
## Otago-Northland            0.149143113 -0.4965566 0.7948428 0.9998150
## Poverty Bay-Northland     -0.480879178 -1.1519100 0.1901516 0.4354502
## Southland-Northland       -0.330456679 -0.9761564 0.3152430 0.8728433
## Tasman-Northland           0.015710662 -0.4587798 0.4902011 1.0000000
## Wairarapa-Northland       -0.234690806 -0.8803905 0.4110089 0.9887859
## Whangarei-Northland       -0.128715101 -0.8705676 0.6131374 0.9999896
## Poverty Bay-Otago         -0.630022291 -1.3830311 0.1229865 0.2043451
## Southland-Otago           -0.479599793 -1.2101256 0.2509260 0.5771100
## Tasman-Otago              -0.133432451 -0.7181384 0.4512735 0.9998356
## Wairarapa-Otago           -0.383833919 -1.1143597 0.3466919 0.8519404
## Whangarei-Otago           -0.277858214 -1.0946109 0.5388945 0.9935230
## Southland-Poverty Bay      0.150422499 -0.6025863 0.9034313 0.9999562
## Tasman-Poverty Bay         0.496589840 -0.1159747 1.1091543 0.2458941
## Wairarapa-Poverty Bay      0.246188372 -0.5068204 0.9991971 0.9953884
## Whangarei-Poverty Bay      0.352164077 -0.4847584 1.1890865 0.9652197
## Tasman-Southland           0.346167341 -0.2385386 0.9308733 0.7254765
## Wairarapa-Southland        0.095765873 -0.6347599 0.8262917 0.9999994
## Whangarei-Southland        0.201741579 -0.6150111 1.0184943 0.9996457
## Wairarapa-Tasman          -0.250401468 -0.8351074 0.3343045 0.9604954
## Whangarei-Tasman          -0.144425763 -0.8338444 0.5449928 0.9999295
## Whangarei-Wairarapa        0.105975705 -0.7107770 0.9227284 0.9999995
#region significant in shannon

#region within a crop
#I did this for beta and then decided I prefer it because of the biased sampling across crops/regions
#radish has only one location
categories_crop <- meta_data_day1 %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
#filtered_sppr <- sppr[grepl('^Apple', names(sppr))]
filtered_shannondiv <- shannondiv[grepl(paste0('^',item), names(shannondiv))]
filtered_meta_data_day1<-meta_data_day1 %>% filter(sample_ID %in% names(filtered_shannondiv))
shannondiv_aov <-aov(filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
print(summary(shannondiv_aov))
print(TukeyHSD(shannondiv_aov))
tukey_test<-TukeyHSD(shannondiv_aov)
tukey_test<-tukey_test$Region
write.csv(tukey_test,paste('Alpha_shannon_diversity_onerep_',item,'vsregion.csv'),quote = F,row.names = T)
}
## [1] "Analysing Apple"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       1 0.0856 0.08557    0.62   0.44
## Residuals   20 2.7591 0.13796               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                        diff        lwr       upr     p adj
## Tasman-Hawkes Bay 0.1296457 -0.2137365 0.4730279 0.4401851
## 
## [1] "Analysing Pear"
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Region       1  1.425   1.425   4.718 0.0506 .
## Residuals   12  3.624   0.302                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                         diff       lwr         upr     p adj
## Tasman-Hawkes Bay -0.7062287 -1.414635 0.002177221 0.0506029
## 
## [1] "Analysing Avocado"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       2  0.144  0.0722   0.266  0.768
## Residuals   46 12.506  0.2719               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                                diff        lwr       upr     p adj
## Northland-Bay of Plenty  0.11222676 -0.2861694 0.5106229 0.7749744
## Whangarei-Bay of Plenty -0.01648834 -0.5864193 0.5534426 0.9972974
## Whangarei-Northland     -0.12871510 -0.7332181 0.4757879 0.8641069
## 
## [1] "Analysing Kiwifruit"
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Region       3  3.713  1.2377   4.285 0.00929 **
## Residuals   48 13.866  0.2889                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                                  diff         lwr       upr     p adj
## Hawkes Bay-Bay of Plenty  -0.40291656 -0.97280687 0.1669738 0.2494385
## Poverty Bay-Bay of Plenty -0.48690386 -1.08120411 0.1073964 0.1433383
## Tasman-Bay of Plenty       0.22664160 -0.26689789 0.7201811 0.6160484
## Poverty Bay-Hawkes Bay    -0.08398731 -0.77904325 0.6110686 0.9883561
## Tasman-Hawkes Bay          0.62955816  0.01841888 1.2406974 0.0412246
## Tasman-Poverty Bay         0.71354546  0.07958251 1.3475084 0.0217384
## 
## [1] "Analysing Pakchoi"
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Region       3  2.433  0.8110   3.332 0.0298 *
## Residuals   37  9.007  0.2434                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                             diff         lwr       upr     p adj
## Canterbury-Auckland    0.5910218 -0.12363480 1.3056784 0.1354441
## Hawkes Bay-Auckland    0.9346287 -0.07894837 1.9482058 0.0798167
## Southland-Auckland     0.2253849 -0.57209327 1.0228631 0.8717124
## Hawkes Bay-Canterbury  0.3436070 -0.46725473 1.1544686 0.6676674
## Southland-Canterbury  -0.3656368 -0.88151436 0.1502407 0.2429283
## Southland-Hawkes Bay  -0.7092438 -1.59396644 0.1754788 0.1546168
## 
## [1] "Analysing Onion"
##             Df Sum Sq Mean Sq F value Pr(>F)
## Region       3  0.495  0.1652   0.909  0.444
## Residuals   46  8.359  0.1817               
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                               diff        lwr       upr     p adj
## Marlborough-Canterbury -0.09754088 -0.4816738 0.2865921 0.9053416
## Otago-Canterbury        0.12255610 -0.4642167 0.7093289 0.9442408
## Wairarapa-Canterbury   -0.22658792 -0.7056859 0.2525100 0.5921865
## Otago-Marlborough       0.22009698 -0.3453314 0.7855254 0.7284405
## Wairarapa-Marlborough  -0.12904705 -0.5817521 0.3236580 0.8719835
## Wairarapa-Otago        -0.34914403 -0.9829310 0.2846430 0.4645013
## 
## [1] "Analysing Carrot"
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Region       1  0.314  0.3139   2.811 0.0993 .
## Residuals   55  6.141  0.1116                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = filtered_shannondiv ~ Region, data = filtered_meta_data_day1)
## 
## $Region
##                       diff         lwr       upr     p adj
## Otago-Canterbury 0.2904911 -0.05672469 0.6377069 0.0992879
#impact of flowering time
shannondiv_aov <-aov(shannondiv ~ flowertime, data = meta_data_day1)
summary(shannondiv_aov)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## flowertime    1  87.18   87.18   270.9 <2e-16 ***
## Residuals   294  94.62    0.32                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#TukeyHSD(shannondiv_aov)

#can plot that out
shannondiv_df <- shannondiv %>% 
  enframe() %>% 
  full_join(meta_data_day1, by = c("name" = "sample_ID"))

#shannondiv_df$Crop <- factor(shannondiv_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
shannondiv_df$Crop <- factor(shannondiv_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

#just crop
plot_sppr <- ggplot(shannondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  scale_fill_manual(values=col_bound)+
  theme_bw()+
  labs(x = "Crop",
       y = "Shannon",
       title = "Species richness: shannon")
plot_sppr

#other significant factor was region
plot_sppr <- ggplot(shannondiv_df, aes(x = Region, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~Crop, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Shannon",
       title = "Species richness: shannon")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

#other significant factor was flowertime
plot_sppr <- ggplot(shannondiv_df, aes(x = Crop, y = value, fill = Crop)) +
  geom_boxplot() +
  facet_wrap(~flowertime, scales = "free_x")+
  theme_bw()+
  scale_fill_manual(values=col_bound)+
  labs(x = "Region",
       y = "Shannon",
       title = "Species richness: shannon")+
  theme(axis.text.x = element_text(angle = 90,size=7,hjust=1))

plot_sppr

Beta diversity and MDS plots

################
#beta diversity#
################
#im not sure that this is useful as I have to remove the 0's (e.g. samples where there was no non-bee insects observed ~mostly apple/pear) but here it is:
#remove zero values:
counts_day1_wide_no0 <- counts_day1_wide[rowSums(counts_day1_wide == 0) != ncol(counts_day1_wide), ]

meta_data_day1_no0<-meta_data_day1 %>% filter(sample_ID %in% row.names(counts_day1_wide_no0))

#crop
perm<-adonis2(counts_day1_wide_no0 ~ Crop,data=meta_data_day1_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_day1_wide_no0 ~ Crop, data = meta_data_day1_no0)
##           Df SumOfSqs     R2      F Pr(>F)    
## Model      7   27.915 0.2488 12.917  0.001 ***
## Residual 273   84.283 0.7512                  
## Total    280  112.198 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pairwise_adonis<-pairwise.adonis2(counts_day1_wide_no0 ~ Crop, data = meta_data_day1_no0)
pairwise_adonis
## $parent_call
## [1] "counts_day1_wide_no0 ~ Crop , strata = Null , permutations 999"
## 
## $Apple_vs_Pear
##          Df SumOfSqs     R2      F Pr(>F)
## Model     1   0.5977 0.0578 1.4722  0.119
## Residual 24   9.7440 0.9422              
## Total    25  10.3417 1.0000              
## 
## $Apple_vs_Avocado
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    2.637 0.10987 7.7762  0.001 ***
## Residual 63   21.364 0.89013                  
## Total    64   24.001 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Kiwifruit
##          Df SumOfSqs      R2     F Pr(>F)   
## Model     1   1.0005 0.04124 2.624  0.004 **
## Residual 61  23.2577 0.95876                
## Total    62  24.2582 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Apple_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   3.0527 0.13808 8.8108  0.001 ***
## Residual 55  19.0557 0.86192                  
## Total    56  22.1084 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.4085 0.12598 3.6035  0.001 ***
## Residual 25   9.7718 0.87402                  
## Total    26  11.1803 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   3.5847 0.15349 11.604  0.001 ***
## Residual 64  19.7698 0.84651                  
## Total    65  23.3544 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Carrot
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1   4.0486 0.1703 14.573  0.001 ***
## Residual 71  19.7254 0.8297                  
## Total    72  23.7740 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Avocado
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.2763 0.06518 3.9742  0.001 ***
## Residual 57  18.3060 0.93482                  
## Total    58  19.5824 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Kiwifruit
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   1.1381 0.05334 3.0988  0.003 **
## Residual 55  20.1999 0.94666                 
## Total    56  21.3380 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pear_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.3829 0.12964 7.2987  0.001 ***
## Residual 49  15.9979 0.87036                  
## Total    50  18.3808 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.1974 0.15135 3.3885  0.001 ***
## Residual 19   6.7140 0.84865                  
## Total    20   7.9114 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.5543 0.13258 8.8649  0.001 ***
## Residual 58  16.7120 0.86742                  
## Total    59  19.2663 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Carrot
##          Df SumOfSqs      R2     F Pr(>F)    
## Model     1   2.7001 0.13941 10.53  0.001 ***
## Residual 65  16.6676 0.86059                 
## Total    66  19.3676 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Kiwifruit
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1    4.843 0.1321 14.307  0.001 ***
## Residual 94   31.820 0.8679                  
## Total    95   36.663 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Avocado_vs_Pak Choi`
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1    6.639 0.1938 21.155  0.001 ***
## Residual 88   27.618 0.8062                  
## Total    89   34.257 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    2.595 0.12399 8.2096  0.001 ***
## Residual 58   18.334 0.87601                  
## Total    59   20.929 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    7.515 0.20964 25.728  0.001 ***
## Residual 97   28.332 0.79036                  
## Total    98   35.847 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1    7.168 0.20218 26.355  0.001 ***
## Residual 104   28.287 0.79782                  
## Total    105   35.456 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Kiwifruit_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    5.812 0.16453 16.936  0.001 ***
## Residual 86   29.512 0.83547                  
## Total    87   35.323 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    1.606 0.07355 4.4461  0.001 ***
## Residual 56   20.228 0.92645                  
## Total    57   21.834 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    7.291 0.19434 22.916  0.001 ***
## Residual 95   30.226 0.80566                  
## Total    96   37.517 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Carrot
##           Df SumOfSqs     R2      F Pr(>F)    
## Model      1    8.642 0.2226 29.207  0.001 ***
## Residual 102   30.181 0.7774                  
## Total    103   38.824 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Radish`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.7268 0.04338 2.2675  0.016 *
## Residual 50  16.0257 0.95662                
## Total    51  16.7524 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Onion`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   3.5144 0.11898 12.019  0.001 ***
## Residual 89  26.0237 0.88102                  
## Total    90  29.5380 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Carrot`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    4.508 0.14787 16.658  0.001 ***
## Residual 96   25.979 0.85213                  
## Total    97   30.487 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Radish_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.6406 0.08926 5.7823  0.001 ***
## Residual 59  16.7398 0.91074                  
## Total    60  18.3804 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Radish_vs_Carrot
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.8926 0.10182 7.4817  0.001 ***
## Residual 66  16.6954 0.89818                  
## Total    67  18.5879 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Onion_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1   2.5064 0.08584 9.8591  0.001 ***
## Residual 105  26.6933 0.91416                  
## Total    106  29.1998 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 

for (item in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }

write.csv(myData,'Pairwise_adonis_beta_diversity_day1repsonly_countsvscrops.csv',quote = F,row.names = F)

#flowertime
perm<-adonis2(counts_day1_wide_no0 ~ flowertime,data=meta_data_day1_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_day1_wide_no0 ~ flowertime, data = meta_data_day1_no0)
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1   12.753 0.11366 35.779  0.001 ***
## Residual 279   99.445 0.88634                  
## Total    280  112.198 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#pairwise_adonis<-pairwise.adonis2(counts_day1_wide_no0 ~ flowertime, data = meta_data_day1_no0)
#pairwise_adonis

#I think to look at the impact of region within a crop we need to run this within a crop. This is because there is little overlap in regions between crops and the 0,0 situation makes it too confusing (at least for me)
#so just testing does community composition vary between regions within a crop. 
categories_crop <- meta_data_day1 %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)

for (item in categories_crop$Crop){
  print(paste('Analysing',item))
filtered_counts_day1_wide_no0 <- counts_day1_wide_no0[grepl(paste0('^',item), rownames(counts_day1_wide_no0)), ] %>% select(where(~ sum(.) != 0))
filtered_meta_data_day1_no0<-meta_data_day1 %>% filter(sample_ID %in% row.names(filtered_counts_day1_wide_no0))
perm<-adonis2(filtered_counts_day1_wide_no0 ~ Region,data=filtered_meta_data_day1_no0)
print(perm)
pairwise_adonis<-pairwise.adonis2(filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
print(pairwise_adonis)
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 
for (item1 in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item1,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item1, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }
write.csv(myData,paste0('Pairwise_adonis_beta_diversity_day1repsonly_regionvs',item,'.csv'),quote = F,row.names = F)
}
## [1] "Analysing Apple"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2     F Pr(>F)   
## Model     1   1.1610 0.18138 3.102  0.002 **
## Residual 14   5.2399 0.81862                
## Total    15   6.4009 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2     F Pr(>F)    
## Model     1   1.1610 0.18138 3.102  0.001 ***
## Residual 14   5.2399 0.81862                 
## Total    15   6.4009 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pear"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.7951 0.23784 2.4965  0.006 **
## Residual  8   2.5480 0.76216                 
## Total     9   3.3431 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.7951 0.23784 2.4965  0.006 **
## Residual  8   2.5480 0.76216                 
## Total     9   3.3431 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Avocado"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     2   1.5319 0.10238 2.6232  0.002 **
## Residual 46  13.4311 0.89762                 
## Total    48  14.9629 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Northland`
##          Df SumOfSqs     R2      F Pr(>F)    
## Model     1   1.0994 0.0823 3.6767  0.001 ***
## Residual 41  12.2601 0.9177                  
## Total    42  13.3596 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Bay of Plenty_vs_Whangarei`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3421 0.03473 1.1155  0.332
## Residual 31   9.5070 0.96527              
## Total    32   9.8491 1.00000              
## 
## $Northland_vs_Whangarei
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.7498 0.12829 2.9434  0.003 **
## Residual 20   5.0950 0.87171                 
## Total    21   5.8448 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Kiwifruit"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   2.4743 0.14678 2.4658  0.001 ***
## Residual 43  14.3825 0.85322                  
## Total    46  16.8568 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Poverty Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3823 0.03951 0.9873  0.427
## Residual 24   9.2930 0.96049              
## Total    25   9.6753 1.00000              
## 
## $`Bay of Plenty_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.9476 0.09237 2.5442  0.011 *
## Residual 25   9.3112 0.90763                
## Total    26  10.2588 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Bay of Plenty_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.9537 0.08065 2.8073  0.008 **
## Residual 32  10.8714 0.91935                 
## Total    33  11.8251 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Poverty Bay_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.9114 0.20608 2.8553  0.036 *
## Residual 11   3.5112 0.79392                
## Total    12   4.4226 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Poverty Bay_vs_Tasman`
##          Df SumOfSqs     R2      F Pr(>F)  
## Model     1   0.6616 0.1154 2.3482  0.034 *
## Residual 18   5.0713 0.8846                
## Total    19   5.7329 1.0000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.1077 0.17874 4.1351  0.001 ***
## Residual 19   5.0895 0.82126                  
## Total    20   6.1972 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pakchoi"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   2.5275 0.19973 3.0781  0.001 ***
## Residual 37  10.1273 0.80027                  
## Total    40  12.6548 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Auckland_vs_Canterbury
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.1250 0.13494 4.2115  0.001 ***
## Residual 27   7.2122 0.86506                  
## Total    28   8.3372 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Auckland_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3954 0.22901 1.4852  0.249
## Residual  5   1.3312 0.77099              
## Total     6   1.7265 1.00000              
## 
## $Auckland_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.9536 0.22868 3.2612  0.003 **
## Residual 11   3.2165 0.77132                 
## Total    12   4.1702 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Canterbury_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.5671 0.07584 2.1335  0.024 *
## Residual 26   6.9107 0.92416                
## Total    27   7.4778 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   0.9286 0.09549 3.3781  0.001 ***
## Residual 32   8.7961 0.90451                  
## Total    33   9.7247 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Southland`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.6992 0.19346 2.3987  0.038 *
## Residual 10   2.9150 0.80654                
## Total    11   3.6142 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Onion"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   1.6693 0.12486 2.1877  0.001 ***
## Residual 46  11.6996 0.87514                  
## Total    49  13.3689 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs      R2     F Pr(>F)  
## Model     1   0.4072 0.09027 1.786  0.048 *
## Residual 18   4.1038 0.90973               
## Total    19   4.5110 1.00000               
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6737 0.07373 2.7063  0.004 **
## Residual 34   8.4640 0.92627                 
## Total    35   9.1378 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6049 0.09762 2.3801   0.01 **
## Residual 22   5.5909 0.90238                 
## Total    23   6.1958 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Otago_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3443 0.05336 1.3528  0.159
## Residual 24   6.1087 0.94664              
## Total    25   6.4530 1.00000              
## 
## $Otago_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.4502 0.12215 1.6698  0.061 .
## Residual 12   3.2355 0.87785                
## Total    13   3.6858 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Marlborough_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.7098 0.08546 2.6166  0.007 **
## Residual 28   7.5958 0.91454                 
## Total    29   8.3056 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Carrot"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs     R2      F Pr(>F)  
## Model     1   0.4344 0.0326 1.8535   0.04 *
## Residual 55  12.8901 0.9674                
## Total    56  13.3245 1.0000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs     R2      F Pr(>F)  
## Model     1   0.4344 0.0326 1.8535  0.037 *
## Residual 55  12.8901 0.9674                
## Total    56  13.3245 1.0000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"
#MDS
pk_NMDS <- metaMDS(counts_day1_wide_no0,trymax=20)
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.1742045 
## Run 1 stress 0.1741919 
## ... New best solution
## ... Procrustes: rmse 0.009913288  max resid 0.05505071 
## Run 2 stress 0.1738884 
## ... New best solution
## ... Procrustes: rmse 0.00643423  max resid 0.04474276 
## Run 3 stress 0.1739235 
## ... Procrustes: rmse 0.01348199  max resid 0.09078369 
## Run 4 stress 0.1740874 
## ... Procrustes: rmse 0.007170677  max resid 0.05399337 
## Run 5 stress 0.1738227 
## ... New best solution
## ... Procrustes: rmse 0.004599909  max resid 0.03795657 
## Run 6 stress 0.1741892 
## ... Procrustes: rmse 0.00811584  max resid 0.06963205 
## Run 7 stress 0.1738668 
## ... Procrustes: rmse 0.01157723  max resid 0.0563795 
## Run 8 stress 0.1748467 
## Run 9 stress 0.1737296 
## ... New best solution
## ... Procrustes: rmse 0.008571698  max resid 0.05197666 
## Run 10 stress 0.1752747 
## Run 11 stress 0.1744465 
## Run 12 stress 0.1747564 
## Run 13 stress 0.1739611 
## ... Procrustes: rmse 0.01287711  max resid 0.06373397 
## Run 14 stress 0.1740124 
## ... Procrustes: rmse 0.005789706  max resid 0.03672755 
## Run 15 stress 0.1747287 
## Run 16 stress 0.1740435 
## ... Procrustes: rmse 0.0056725  max resid 0.04662368 
## Run 17 stress 0.1752499 
## Run 18 stress 0.1741146 
## ... Procrustes: rmse 0.0115926  max resid 0.06139293 
## Run 19 stress 0.1746273 
## Run 20 stress 0.1737481 
## ... Procrustes: rmse 0.003671995  max resid 0.02473057 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##     18: no. of iterations >= maxit
##      2: stress ratio > sratmax
#doesnt converge - whats up?
pk_NMDS
## 
## Call:
## metaMDS(comm = counts_day1_wide_no0, trymax = 20) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     wisconsin(sqrt(counts_day1_wide_no0)) 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.1737296 
## Stress type 1, weak ties
## Best solution was not repeated after 20 tries
## The best solution was from try 9 (random start)
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'wisconsin(sqrt(counts_day1_wide_no0))'
#stress (ideally <0.2)

stressplot(pk_NMDS)

plot(pk_NMDS)

plot_df <- scores(pk_NMDS, display = "sites") %>% 
  as.data.frame() %>% 
  rownames_to_column("site") %>% 
  full_join(meta_data_day1_no0, by = c("site" = "sample_ID"))

#keep same colour scheme
#plot_df$Crop <- factor(plot_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
plot_df$Crop <- factor(plot_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 0.8) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  labs(title = "NMDS")
plot_nmds
## Warning in MASS::cov.trob(data[, vars]): Probable convergence failure

#################################################################
#setting a minimum count of 5 across at least 2 taxa#
#################################################################
#this is a little arbitary but means the MDS plot isnt trying to converge stuff with basically no data
#remove <5 counts:
counts_day1_wide_no0 <- counts_day1_wide[rowSums(counts_day1_wide) >= 5, ]
#remove things that are only from a single species
counts_day1_wide_no0 <-counts_day1_wide_no0[which(rowSums(1*(counts_day1_wide_no0!=0))>1),]

meta_data_day1_no0<-meta_data_day1 %>% filter(sample_ID %in% row.names(counts_day1_wide_no0))

#redoing the beta as well
#crop
perm<-adonis2(counts_day1_wide_no0 ~ Crop,data=meta_data_day1_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_day1_wide_no0 ~ Crop, data = meta_data_day1_no0)
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      7   23.880 0.26369 11.921  0.001 ***
## Residual 233   66.679 0.73631                  
## Total    240   90.559 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pairwise_adonis<-pairwise.adonis2(counts_day1_wide_no0 ~ Crop, data = meta_data_day1_no0)
pairwise_adonis
## $parent_call
## [1] "counts_day1_wide_no0 ~ Crop , strata = Null , permutations 999"
## 
## $Apple_vs_Pear
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1  0.27733 0.09104 0.8012   0.62
## Residual  8  2.76903 0.90896              
## Total     9  3.04636 1.00000              
## 
## $Apple_vs_Avocado
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   1.0069 0.06211 3.2452  0.003 **
## Residual 49  15.2036 0.93789                 
## Total    50  16.2105 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Kiwifruit
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.5443 0.05616 1.6661  0.092 .
## Residual 28   9.1464 0.94384                
## Total    29   9.6906 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Apple_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    1.156 0.07684 3.5792  0.001 ***
## Residual 43   13.888 0.92316                  
## Total    44   15.044 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.6252 0.11955 1.7652  0.062 .
## Residual 13   4.6040 0.88045                
## Total    14   5.2292 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.2963 0.08526 4.7533  0.001 ***
## Residual 51  13.9091 0.91474                  
## Total    52  15.2054 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Apple_vs_Carrot
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.3765 0.08638 5.5786  0.001 ***
## Residual 59  14.5576 0.91362                  
## Total    60  15.9340 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Avocado
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   0.9155 0.05575 3.0111  0.001 ***
## Residual 51  15.5064 0.94425                  
## Total    52  16.4219 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Kiwifruit
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.1788 0.11092 3.7426  0.001 ***
## Residual 30   9.4491 0.88908                  
## Total    31  10.6280 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pear_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.8414 0.11486 5.8392  0.001 ***
## Residual 45  14.1907 0.88514                  
## Total    46  16.0321 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   1.0853 0.18113 3.3179  0.002 **
## Residual 15   4.9068 0.81887                 
## Total    16   5.9922 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.9054 0.11822 7.1058  0.001 ***
## Residual 53  14.2118 0.88178                  
## Total    54  16.1173 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Pear_vs_Carrot
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.8738 0.11197 7.6917  0.001 ***
## Residual 61  14.8604 0.88803                  
## Total    62  16.7342 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Kiwifruit
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   3.5813 0.14064 11.619  0.001 ***
## Residual 71  21.8837 0.85936                  
## Total    72  25.4651 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Avocado_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    6.631 0.19938 21.417  0.001 ***
## Residual 86   26.625 0.80062                  
## Total    87   33.256 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   2.6249 0.13147 8.4765  0.001 ***
## Residual 56  17.3414 0.86853                  
## Total    57  19.9663 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Onion
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    7.541 0.22059 26.604  0.001 ***
## Residual 94   26.646 0.77941                  
## Total    95   34.188 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Avocado_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1    7.058 0.20545 26.375  0.001 ***
## Residual 102   27.295 0.79455                  
## Total    103   34.353 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Kiwifruit_vs_Pak Choi`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   4.5383 0.18076 14.342  0.001 ***
## Residual 65  20.5680 0.81924                  
## Total    66  25.1063 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Radish
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.4015 0.11048 4.3469  0.001 ***
## Residual 35  11.2841 0.88952                  
## Total    36  12.6856 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Onion
##          Df SumOfSqs      R2     F Pr(>F)    
## Model     1    5.621 0.21446 19.93  0.001 ***
## Residual 73   20.589 0.78554                 
## Total    74   26.210 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Kiwifruit_vs_Carrot
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   6.3615 0.23049 24.262  0.001 ***
## Residual 81  21.2377 0.76951                  
## Total    82  27.5992 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Radish`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.7268 0.04338 2.2675  0.011 *
## Residual 50  16.0257 0.95662                
## Total    51  16.7524 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Onion`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   3.5642 0.12335 12.382  0.001 ***
## Residual 88  25.3307 0.87665                  
## Total    89  28.8950 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Pak Choi_vs_Carrot`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1    4.508 0.14787 16.658  0.001 ***
## Residual 96   25.979 0.85213                  
## Total    97   30.487 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Radish_vs_Onion
##          Df SumOfSqs    R2      F Pr(>F)    
## Model     1   1.6844 0.095 6.0881  0.001 ***
## Residual 58  16.0468 0.905                  
## Total    59  17.7312 1.000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Radish_vs_Carrot
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.8926 0.10182 7.4817  0.001 ***
## Residual 66  16.6954 0.89818                  
## Total    67  18.5879 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Onion_vs_Carrot
##           Df SumOfSqs      R2      F Pr(>F)    
## Model      1   2.5059 0.08791 10.023  0.001 ***
## Residual 104  26.0004 0.91209                  
## Total    105  28.5063 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 

for (item in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }

write.csv(myData,'Pairwise_adonis_beta_diversity_day1repsonly_countsvscrops_mincount5.csv',quote = F,row.names = F)

#flowertime
perm<-adonis2(counts_day1_wide_no0 ~ flowertime,data=meta_data_day1_no0)
perm
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = counts_day1_wide_no0 ~ flowertime, data = meta_data_day1_no0)
##           Df SumOfSqs     R2      F Pr(>F)    
## Model      1   10.641 0.1175 31.823  0.001 ***
## Residual 239   79.918 0.8825                  
## Total    240   90.559 1.0000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#pairwise_adonis<-pairwise.adonis2(counts_day1_wide_no0 ~ flowertime, data = meta_data_day1_no00)
#pairwise_adonis

#I think to look at the impact of region within a crop we need to run this within a crop. This is because there is little overlap in regions between crops and the 0,0 situation makes it too confusing (at least for me)
#so just testing does community composition vary between regions within a crop. 
categories_crop <- meta_data_day1 %>% filter(Crop!='Radish') %>% select(Crop) %>% unique()
categories_crop$Crop <- gsub("Pak Choi", "Pakchoi", categories_crop$Crop)
for (item in categories_crop$Crop){
  print(paste('Analysing',item))
filtered_counts_day1_wide_no0<- counts_day1_wide_no0[grepl(paste0('^',item), rownames(counts_day1_wide_no0)), ] %>% select(where(~ sum(.) != 0))
filtered_meta_data_day1_no0<-meta_data_day1 %>% filter(sample_ID %in% row.names(filtered_counts_day1_wide_no0))
perm<-adonis2(filtered_counts_day1_wide_no0 ~ Region,data=filtered_meta_data_day1_no0)
print(perm)
pairwise_adonis<-pairwise.adonis2(filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
print(pairwise_adonis)
tests<-names(pairwise_adonis)
tests<-tests[-1]
columns= c("Test","pvalue")
myData = data.frame(matrix(nrow = 0, ncol = length(columns))) 
for (item1 in tests){
#  print(item)
   pvalue<-eval(parse(text =paste0('pairwise_adonis$','`',item1,'`' ,'$`Pr(>F)`[1]')))
#   print(pvalue)
out<-data.frame(Test = item1, pvalue = pvalue)
#print(out)
myData<-rbind(myData,out)
      }
write.csv(myData,paste0('Pairwise_adonis_beta_diversity_day1repsonly_regionvs',item,'_mincount5.csv'),quote = F,row.names = F)
}
## [1] "Analysing Apple"
## 'nperm' >= set of all permutations: complete enumeration.
## Set of permutations < 'minperm'. Generating entire set.
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 23
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1  0.31711 0.25716 0.6924      1
## Residual  2  0.91602 0.74284              
## Total     3  1.23312 1.00000
## 'nperm' >= set of all permutations: complete enumeration.
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1  0.31711 0.25716 0.6924      1
## Residual  2  0.91602 0.74284              
## Total     3  1.23312 1.00000              
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pear"
## 'nperm' >= set of all permutations: complete enumeration.
## Set of permutations < 'minperm'. Generating entire set.
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 719
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F  Pr(>F)  
## Model     1  0.59633 0.38826 2.5387 0.06667 .
## Residual  4  0.93958 0.61174                 
## Total     5  1.53591 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 'nperm' >= set of all permutations: complete enumeration.
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F  Pr(>F)  
## Model     1  0.59633 0.38826 2.5387 0.06667 .
## Residual  4  0.93958 0.61174                 
## Total     5  1.53591 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Avocado"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     2   1.5275 0.10934 2.7008  0.002 **
## Residual 44  12.4430 0.89066                 
## Total    46  13.9705 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Northland`
##          Df SumOfSqs      R2     F Pr(>F)    
## Model     1   1.1261 0.09082 3.896  0.001 ***
## Residual 39  11.2720 0.90918                 
## Total    40  12.3981 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Bay of Plenty_vs_Whangarei`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3181 0.03422 1.0631  0.374
## Residual 30   8.9773 0.96578              
## Total    31   9.2955 1.00000              
## 
## $Northland_vs_Whangarei
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.7355 0.13691 3.0139  0.003 **
## Residual 19   4.6366 0.86309                 
## Total    20   5.3721 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Kiwifruit"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     3   1.8535 0.23423 2.2431  0.002 **
## Residual 22   6.0597 0.76577                 
## Total    25   7.9132 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 'nperm' >= set of all permutations: complete enumeration.
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $`Bay of Plenty_vs_Poverty Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.1285 0.03484 0.3971  0.975
## Residual 11   3.5586 0.96516              
## Total    12   3.6870 1.00000              
## 
## $`Bay of Plenty_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.5004 0.12167 1.5238  0.167
## Residual 11   3.6120 0.87833              
## Total    12   4.1124 1.00000              
## 
## $`Bay of Plenty_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.2604 0.18801 4.6307  0.001 ***
## Residual 20   5.4438 0.81199                  
## Total    21   6.7042 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Poverty Bay_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1  0.35776 0.36742 1.1616 0.6667
## Residual  2  0.61596 0.63258              
## Total     3  0.97372 1.00000              
## 
## $`Poverty Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1  0.52261 0.17595 2.3486  0.042 *
## Residual 11  2.44769 0.82405                
## Total    12  2.97030 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Tasman`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1  0.31247 0.11106 1.3742  0.187
## Residual 11  2.50115 0.88894              
## Total    12  2.81362 1.00000              
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Pakchoi"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   2.5275 0.19973 3.0781  0.001 ***
## Residual 37  10.1273 0.80027                  
## Total    40  12.6548 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Set of permutations < 'minperm'. Generating entire set.
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Auckland_vs_Canterbury
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   1.1250 0.13494 4.2115  0.001 ***
## Residual 27   7.2122 0.86506                  
## Total    28   8.3372 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Auckland_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3954 0.22901 1.4852  0.225
## Residual  5   1.3312 0.77099              
## Total     6   1.7265 1.00000              
## 
## $Auckland_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.9536 0.22868 3.2612  0.004 **
## Residual 11   3.2165 0.77132                 
## Total    12   4.1702 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Canterbury_vs_Hawkes Bay`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.5671 0.07584 2.1335  0.023 *
## Residual 26   6.9107 0.92416                
## Total    27   7.4778 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Southland
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     1   0.9286 0.09549 3.3781  0.001 ***
## Residual 32   8.7961 0.90451                  
## Total    33   9.7247 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $`Hawkes Bay_vs_Southland`
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1   0.6992 0.19346 2.3987   0.04 *
## Residual 10   2.9150 0.80654                
## Total    11   3.6142 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Onion"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs      R2      F Pr(>F)    
## Model     3   1.6258 0.12826 2.2069  0.001 ***
## Residual 45  11.0501 0.87174                  
## Total    48  12.6759 1.00000                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs      R2     F Pr(>F)  
## Model     1   0.4072 0.09027 1.786  0.045 *
## Residual 18   4.1038 0.90973               
## Total    19   4.5110 1.00000               
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6737 0.07373 2.7063   0.01 **
## Residual 34   8.4640 0.92627                 
## Total    35   9.1378 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Canterbury_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.5556 0.10107 2.3612  0.008 **
## Residual 21   4.9415 0.89893                 
## Total    22   5.4971 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Otago_vs_Marlborough
##          Df SumOfSqs      R2      F Pr(>F)
## Model     1   0.3443 0.05336 1.3528  0.188
## Residual 24   6.1087 0.94664              
## Total    25   6.4530 1.00000              
## 
## $Otago_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)  
## Model     1  0.42663 0.14161 1.8147   0.07 .
## Residual 11  2.58610 0.85839                
## Total    12  3.01273 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $Marlborough_vs_Wairarapa
##          Df SumOfSqs      R2      F Pr(>F)   
## Model     1   0.6893 0.09027 2.6792   0.01 **
## Residual 27   6.9463 0.90973                 
## Total    28   7.6356 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"      
## [1] "Analysing Carrot"
## Permutation test for adonis under reduced model
## Permutation: free
## Number of permutations: 999
## 
## adonis2(formula = filtered_counts_day1_wide_no0 ~ Region, data = filtered_meta_data_day1_no0)
##          Df SumOfSqs     R2      F Pr(>F)  
## Model     1   0.4344 0.0326 1.8535   0.03 *
## Residual 55  12.8901 0.9674                
## Total    56  13.3245 1.0000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## $parent_call
## [1] "filtered_counts_day1_wide_no0 ~ Region , strata = Null , permutations 999"
## 
## $Canterbury_vs_Otago
##          Df SumOfSqs     R2      F Pr(>F)  
## Model     1   0.4344 0.0326 1.8535   0.04 *
## Residual 55  12.8901 0.9674                
## Total    56  13.3245 1.0000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## attr(,"class")
## [1] "pwadstrata" "list"
#####
#MDS#
#####

pk_NMDS <- metaMDS(counts_day1_wide_no0,trymax=500)
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.2378012 
## Run 1 stress 0.2758738 
## Run 2 stress 0.2388144 
## Run 3 stress 0.2443292 
## Run 4 stress 0.2390363 
## Run 5 stress 0.2573102 
## Run 6 stress 0.2474687 
## Run 7 stress 0.2397428 
## Run 8 stress 0.2378306 
## ... Procrustes: rmse 0.001746814  max resid 0.01613573 
## Run 9 stress 0.2404073 
## Run 10 stress 0.2751305 
## Run 11 stress 0.2408579 
## Run 12 stress 0.2381609 
## ... Procrustes: rmse 0.00445632  max resid 0.06699434 
## Run 13 stress 0.2412538 
## Run 14 stress 0.2380438 
## ... Procrustes: rmse 0.00686162  max resid 0.06672309 
## Run 15 stress 0.2421597 
## Run 16 stress 0.2399348 
## Run 17 stress 0.2381025 
## ... Procrustes: rmse 0.009545383  max resid 0.1350442 
## Run 18 stress 0.2776889 
## Run 19 stress 0.2380405 
## ... Procrustes: rmse 0.006833851  max resid 0.06673315 
## Run 20 stress 0.2408359 
## Run 21 stress 0.2790709 
## Run 22 stress 0.2386308 
## Run 23 stress 0.2448597 
## Run 24 stress 0.2398169 
## Run 25 stress 0.2385605 
## Run 26 stress 0.2452983 
## Run 27 stress 0.2394035 
## Run 28 stress 0.238869 
## Run 29 stress 0.2415471 
## Run 30 stress 0.2428658 
## Run 31 stress 0.240129 
## Run 32 stress 0.2460718 
## Run 33 stress 0.2381044 
## ... Procrustes: rmse 0.005787396  max resid 0.05857116 
## Run 34 stress 0.2652479 
## Run 35 stress 0.239446 
## Run 36 stress 0.2387149 
## Run 37 stress 0.2400752 
## Run 38 stress 0.2429264 
## Run 39 stress 0.2403784 
## Run 40 stress 0.2720263 
## Run 41 stress 0.2387018 
## Run 42 stress 0.2510884 
## Run 43 stress 0.2382654 
## ... Procrustes: rmse 0.01001398  max resid 0.1349484 
## Run 44 stress 0.2391271 
## Run 45 stress 0.2407738 
## Run 46 stress 0.2399687 
## Run 47 stress 0.2414134 
## Run 48 stress 0.2400651 
## Run 49 stress 0.2473941 
## Run 50 stress 0.2383868 
## Run 51 stress 0.2427548 
## Run 52 stress 0.2377061 
## ... New best solution
## ... Procrustes: rmse 0.005209985  max resid 0.05334213 
## Run 53 stress 0.2378003 
## ... Procrustes: rmse 0.003310026  max resid 0.04728152 
## Run 54 stress 0.2390956 
## Run 55 stress 0.2402813 
## Run 56 stress 0.2380848 
## ... Procrustes: rmse 0.01323656  max resid 0.1351793 
## Run 57 stress 0.2399363 
## Run 58 stress 0.2441241 
## Run 59 stress 0.2381016 
## ... Procrustes: rmse 0.01334711  max resid 0.1352235 
## Run 60 stress 0.2390654 
## Run 61 stress 0.2467184 
## Run 62 stress 0.2401337 
## Run 63 stress 0.2451346 
## Run 64 stress 0.2414647 
## Run 65 stress 0.2388181 
## Run 66 stress 0.2422516 
## Run 67 stress 0.2416757 
## Run 68 stress 0.2439148 
## Run 69 stress 0.2402291 
## Run 70 stress 0.2475161 
## Run 71 stress 0.2409379 
## Run 72 stress 0.239157 
## Run 73 stress 0.2390579 
## Run 74 stress 0.2802639 
## Run 75 stress 0.2391443 
## Run 76 stress 0.2393274 
## Run 77 stress 0.2431909 
## Run 78 stress 0.2381016 
## ... Procrustes: rmse 0.01371302  max resid 0.1352075 
## Run 79 stress 0.2404269 
## Run 80 stress 0.2409924 
## Run 81 stress 0.2381048 
## ... Procrustes: rmse 0.008657389  max resid 0.1176209 
## Run 82 stress 0.2401405 
## Run 83 stress 0.238101 
## ... Procrustes: rmse 0.01337983  max resid 0.1352163 
## Run 84 stress 0.2392874 
## Run 85 stress 0.2412878 
## Run 86 stress 0.238099 
## ... Procrustes: rmse 0.008571599  max resid 0.1175787 
## Run 87 stress 0.2814866 
## Run 88 stress 0.238074 
## ... Procrustes: rmse 0.01318344  max resid 0.1351341 
## Run 89 stress 0.2413691 
## Run 90 stress 0.2381447 
## ... Procrustes: rmse 0.01055635  max resid 0.1351358 
## Run 91 stress 0.2388232 
## Run 92 stress 0.2380115 
## ... Procrustes: rmse 0.007964465  max resid 0.117843 
## Run 93 stress 0.2388289 
## Run 94 stress 0.2377062 
## ... Procrustes: rmse 0.000695841  max resid 0.005413691 
## ... Similar to previous best
## *** Best solution repeated 1 times
#converges - yay - a little stressed but is a lot of data
pk_NMDS
## 
## Call:
## metaMDS(comm = counts_day1_wide_no0, trymax = 500) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     wisconsin(sqrt(counts_day1_wide_no0)) 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.2377061 
## Stress type 1, weak ties
## Best solution was repeated 1 time in 94 tries
## The best solution was from try 52 (random start)
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'wisconsin(sqrt(counts_day1_wide_no0))'
#stress 
stressplot(pk_NMDS)

plot(pk_NMDS)

plot_df <- scores(pk_NMDS, display = "sites") %>% 
  as.data.frame() %>% 
  rownames_to_column("site") %>% 
  full_join(meta_data_day1_no0, by = c("site" = "sample_ID"))

#plot_df$Crop <- factor(plot_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
plot_df$Crop <- factor(plot_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 0.8) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 1) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 3, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 15, "Apple" = 15, "Pear" = 15, "Kiwifruit" = 15, "Pak Choi" = 16, "Radish" = 16, "Carrot" = 16, "Onion" = 16))+
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 2, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 16, "Apple" = 16, "Pear" = 16, "Kiwifruit" = 16, "Pak Choi" = 15, "Radish" = 15, "Carrot" = 15, "Onion" = 15))+
  stat_ellipse(linetype = 1, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

#apple makes that look a bit dumb
#its only got a few samples which is why its so biased to that outlier. I'd just delete the cirle for apple on the previous as i think its too dodgy -

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 2, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 16, "Apple" = 16, "Pear" = 16, "Kiwifruit" = 16, "Pak Choi" = 15, "Radish" = 15, "Carrot" = 15, "Onion" = 15))+
  stat_ellipse(linewidth = 0.5,aes(linetype=Crop)) +
  scale_linetype_manual(values = c(1,0,1,1,1,1,1,1)) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds

# If you set the min at 6 it drops that outlier and wont draw a circle as there is to few points for apple 

counts_day1_wide_no0 <- counts_day1_wide[rowSums(counts_day1_wide) >= 6, ]
#remove things that are only from a single species
counts_day1_wide_no0 <-counts_day1_wide_no0[which(rowSums(1*(counts_day1_wide_no0!=0))>1),]

meta_data_day1_no0<-meta_data_day1 %>% filter(sample_ID %in% row.names(counts_day1_wide_no0))
#MDS
pk_NMDS <- metaMDS(counts_day1_wide_no0,trymax=200)
## Square root transformation
## Wisconsin double standardization
## Run 0 stress 0.2376334 
## Run 1 stress 0.2384927 
## Run 2 stress 0.2424308 
## Run 3 stress 0.2419076 
## Run 4 stress 0.2389921 
## Run 5 stress 0.2427326 
## Run 6 stress 0.239079 
## Run 7 stress 0.2730656 
## Run 8 stress 0.2448312 
## Run 9 stress 0.2390658 
## Run 10 stress 0.2376368 
## ... Procrustes: rmse 0.007898849  max resid 0.0696041 
## Run 11 stress 0.2435468 
## Run 12 stress 0.2456612 
## Run 13 stress 0.2403293 
## Run 14 stress 0.2712284 
## Run 15 stress 0.2384928 
## Run 16 stress 0.2410051 
## Run 17 stress 0.2446873 
## Run 18 stress 0.2412981 
## Run 19 stress 0.2483773 
## Run 20 stress 0.2440246 
## Run 21 stress 0.2717166 
## Run 22 stress 0.240338 
## Run 23 stress 0.2402729 
## Run 24 stress 0.2378641 
## ... Procrustes: rmse 0.01027951  max resid 0.1227496 
## Run 25 stress 0.2424577 
## Run 26 stress 0.2399013 
## Run 27 stress 0.2397159 
## Run 28 stress 0.2409032 
## Run 29 stress 0.2424961 
## Run 30 stress 0.2376594 
## ... Procrustes: rmse 0.006591371  max resid 0.0697555 
## Run 31 stress 0.2397419 
## Run 32 stress 0.2375283 
## ... New best solution
## ... Procrustes: rmse 0.002532341  max resid 0.03293485 
## Run 33 stress 0.2454572 
## Run 34 stress 0.2384927 
## Run 35 stress 0.2468134 
## Run 36 stress 0.2377779 
## ... Procrustes: rmse 0.00735594  max resid 0.0700589 
## Run 37 stress 0.2743363 
## Run 38 stress 0.269266 
## Run 39 stress 0.2427951 
## Run 40 stress 0.2422429 
## Run 41 stress 0.2773211 
## Run 42 stress 0.2407808 
## Run 43 stress 0.2380439 
## Run 44 stress 0.2402332 
## Run 45 stress 0.2373975 
## ... New best solution
## ... Procrustes: rmse 0.00562685  max resid 0.056937 
## Run 46 stress 0.2522656 
## Run 47 stress 0.2613746 
## Run 48 stress 0.242316 
## Run 49 stress 0.2377278 
## ... Procrustes: rmse 0.01275454  max resid 0.1426887 
## Run 50 stress 0.286028 
## Run 51 stress 0.2378456 
## ... Procrustes: rmse 0.00875887  max resid 0.07006082 
## Run 52 stress 0.2409389 
## Run 53 stress 0.2436376 
## Run 54 stress 0.250289 
## Run 55 stress 0.2432705 
## Run 56 stress 0.2473091 
## Run 57 stress 0.2761395 
## Run 58 stress 0.2379687 
## Run 59 stress 0.2409205 
## Run 60 stress 0.2381663 
## Run 61 stress 0.238075 
## Run 62 stress 0.259727 
## Run 63 stress 0.2412031 
## Run 64 stress 0.2407882 
## Run 65 stress 0.2414526 
## Run 66 stress 0.2449933 
## Run 67 stress 0.2412247 
## Run 68 stress 0.2376066 
## ... Procrustes: rmse 0.007444668  max resid 0.06516947 
## Run 69 stress 0.2438985 
## Run 70 stress 0.2570145 
## Run 71 stress 0.248685 
## Run 72 stress 0.2412298 
## Run 73 stress 0.2390522 
## Run 74 stress 0.2440053 
## Run 75 stress 0.2592219 
## Run 76 stress 0.2403225 
## Run 77 stress 0.2433894 
## Run 78 stress 0.2602514 
## Run 79 stress 0.2775988 
## Run 80 stress 0.2433927 
## Run 81 stress 0.2404266 
## Run 82 stress 0.2450206 
## Run 83 stress 0.2402336 
## Run 84 stress 0.2381677 
## Run 85 stress 0.274583 
## Run 86 stress 0.2488718 
## Run 87 stress 0.2424638 
## Run 88 stress 0.2397421 
## Run 89 stress 0.2413943 
## Run 90 stress 0.2445654 
## Run 91 stress 0.2459383 
## Run 92 stress 0.2378735 
## ... Procrustes: rmse 0.00825596  max resid 0.1233702 
## Run 93 stress 0.2398114 
## Run 94 stress 0.2381736 
## Run 95 stress 0.2391284 
## Run 96 stress 0.2454567 
## Run 97 stress 0.2419211 
## Run 98 stress 0.2384921 
## Run 99 stress 0.2404427 
## Run 100 stress 0.2375183 
## ... Procrustes: rmse 0.005719828  max resid 0.05737251 
## Run 101 stress 0.2378539 
## ... Procrustes: rmse 0.008715055  max resid 0.07007264 
## Run 102 stress 0.2432587 
## Run 103 stress 0.2409844 
## Run 104 stress 0.2401132 
## Run 105 stress 0.2559711 
## Run 106 stress 0.2449837 
## Run 107 stress 0.2388654 
## Run 108 stress 0.2378549 
## ... Procrustes: rmse 0.008293612  max resid 0.1232403 
## Run 109 stress 0.2376227 
## ... Procrustes: rmse 0.006205019  max resid 0.05710754 
## Run 110 stress 0.2403512 
## Run 111 stress 0.246265 
## Run 112 stress 0.2440402 
## Run 113 stress 0.2460693 
## Run 114 stress 0.2401189 
## Run 115 stress 0.241233 
## Run 116 stress 0.2440839 
## Run 117 stress 0.2493573 
## Run 118 stress 0.2424261 
## Run 119 stress 0.2656262 
## Run 120 stress 0.2378178 
## ... Procrustes: rmse 0.01210936  max resid 0.1423696 
## Run 121 stress 0.2420289 
## Run 122 stress 0.280029 
## Run 123 stress 0.2376603 
## ... Procrustes: rmse 0.006022004  max resid 0.0700955 
## Run 124 stress 0.2749764 
## Run 125 stress 0.2376069 
## ... Procrustes: rmse 0.004803886  max resid 0.07017056 
## Run 126 stress 0.2419189 
## Run 127 stress 0.2409292 
## Run 128 stress 0.2462666 
## Run 129 stress 0.2375268 
## ... Procrustes: rmse 0.005619069  max resid 0.0569673 
## Run 130 stress 0.2375829 
## ... Procrustes: rmse 0.005729844  max resid 0.05688859 
## Run 131 stress 0.2525758 
## Run 132 stress 0.2378003 
## ... Procrustes: rmse 0.01210243  max resid 0.1422983 
## Run 133 stress 0.2629855 
## Run 134 stress 0.250366 
## Run 135 stress 0.2379684 
## Run 136 stress 0.2402339 
## Run 137 stress 0.2432122 
## Run 138 stress 0.2376084 
## ... Procrustes: rmse 0.007451546  max resid 0.06523173 
## Run 139 stress 0.2409942 
## Run 140 stress 0.2375208 
## ... Procrustes: rmse 0.005734811  max resid 0.05720373 
## Run 141 stress 0.2388495 
## Run 142 stress 0.2375404 
## ... Procrustes: rmse 0.005687434  max resid 0.05751529 
## Run 143 stress 0.2376063 
## ... Procrustes: rmse 0.00479671  max resid 0.07014551 
## Run 144 stress 0.2375782 
## ... Procrustes: rmse 0.00350692  max resid 0.05172213 
## Run 145 stress 0.2419136 
## Run 146 stress 0.2433711 
## Run 147 stress 0.2423058 
## Run 148 stress 0.2766062 
## Run 149 stress 0.2378447 
## ... Procrustes: rmse 0.00876261  max resid 0.0700771 
## Run 150 stress 0.2374034 
## ... Procrustes: rmse 0.001738193  max resid 0.01406857 
## Run 151 stress 0.2376451 
## ... Procrustes: rmse 0.004905909  max resid 0.07021101 
## Run 152 stress 0.2505707 
## Run 153 stress 0.2433926 
## Run 154 stress 0.2393344 
## Run 155 stress 0.2411317 
## Run 156 stress 0.237773 
## ... Procrustes: rmse 0.01125971  max resid 0.1423973 
## Run 157 stress 0.2454501 
## Run 158 stress 0.2375523 
## ... Procrustes: rmse 0.005837982  max resid 0.05698495 
## Run 159 stress 0.2506548 
## Run 160 stress 0.2402782 
## Run 161 stress 0.2379686 
## Run 162 stress 0.241129 
## Run 163 stress 0.239379 
## Run 164 stress 0.2402556 
## Run 165 stress 0.2384928 
## Run 166 stress 0.2400587 
## Run 167 stress 0.2438917 
## Run 168 stress 0.2749806 
## Run 169 stress 0.2376431 
## ... Procrustes: rmse 0.009511241  max resid 0.1428278 
## Run 170 stress 0.2502348 
## Run 171 stress 0.237855 
## ... Procrustes: rmse 0.008293446  max resid 0.1232296 
## Run 172 stress 0.240071 
## Run 173 stress 0.2704945 
## Run 174 stress 0.2402241 
## Run 175 stress 0.2432453 
## Run 176 stress 0.2401896 
## Run 177 stress 0.2400808 
## Run 178 stress 0.2548766 
## Run 179 stress 0.2407476 
## Run 180 stress 0.2403575 
## Run 181 stress 0.237371 
## ... New best solution
## ... Procrustes: rmse 0.001189537  max resid 0.01216646 
## Run 182 stress 0.2411862 
## Run 183 stress 0.2417426 
## Run 184 stress 0.2415278 
## Run 185 stress 0.2736192 
## Run 186 stress 0.2595163 
## Run 187 stress 0.2611282 
## Run 188 stress 0.2618645 
## Run 189 stress 0.242585 
## Run 190 stress 0.2385059 
## Run 191 stress 0.2867988 
## Run 192 stress 0.2382101 
## Run 193 stress 0.2386222 
## Run 194 stress 0.2397418 
## Run 195 stress 0.2399745 
## Run 196 stress 0.258668 
## Run 197 stress 0.2480442 
## Run 198 stress 0.2789674 
## Run 199 stress 0.2573066 
## Run 200 stress 0.2499681 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##     27: no. of iterations >= maxit
##    168: stress ratio > sratmax
##      5: scale factor of the gradient < sfgrmin
#converges - yay - a little stressed but is a lot of data
pk_NMDS
## 
## Call:
## metaMDS(comm = counts_day1_wide_no0, trymax = 200) 
## 
## global Multidimensional Scaling using monoMDS
## 
## Data:     wisconsin(sqrt(counts_day1_wide_no0)) 
## Distance: bray 
## 
## Dimensions: 2 
## Stress:     0.237371 
## Stress type 1, weak ties
## Best solution was not repeated after 200 tries
## The best solution was from try 181 (random start)
## Scaling: centring, PC rotation, halfchange scaling 
## Species: expanded scores based on 'wisconsin(sqrt(counts_day1_wide_no0))'
#stress 
stressplot(pk_NMDS)

plot(pk_NMDS)

plot_df <- scores(pk_NMDS, display = "sites") %>% 
  as.data.frame() %>% 
  rownames_to_column("site") %>% 
  full_join(meta_data_day1_no0, by = c("site" = "sample_ID"))

#plot_df$Crop <- factor(plot_df$Crop, levels = c("Apple","Pear","Avocado","Kiwifruit","Pak Choi","Carrot", "Onion","Radish"))
plot_df$Crop <- factor(plot_df$Crop, levels = c("Avocado","Apple","Pear","Kiwifruit","Pak Choi","Radish", "Carrot","Onion"))

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 0.8) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_path()`).

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop)) +
  geom_point(size = 2, alpha = 1) +
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_path()`).

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 3, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 15, "Apple" = 15, "Pear" = 15, "Kiwifruit" = 15, "Pak Choi" = 16, "Radish" = 16, "Carrot" = 16, "Onion" = 16))+
  stat_ellipse(linetype = 2, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_path()`).

plot_nmds <- ggplot(plot_df, aes(x = NMDS1, y = NMDS2, color = Crop,shape=Crop)) +
  geom_point(size = 2, alpha = 1) +
  scale_shape_manual(values = c("Avocado" = 16, "Apple" = 16, "Pear" = 16, "Kiwifruit" = 16, "Pak Choi" = 15, "Radish" = 15, "Carrot" = 15, "Onion" = 15))+
  stat_ellipse(linetype = 1, linewidth = 0.5) +
  theme_bw()+
  scale_colour_manual(values=col_bound)+
  labs(title = "NMDS")
plot_nmds
## Too few points to calculate an ellipse
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_path()`).

##innext day 1

########
#innext#
########
#innext by its nature should be robust for sampling bias but just for evenness sake Im rerunning on day 1 samples alone
library(iNEXT.3D)

#generate a list of the unique sample locations
categories <- unique(meta_data$Crop)
categories
## [1] "Apple"     "Pear"      "Avocado"   "Kiwifruit" "Pak Choi"  "Radish"   
## [7] "Onion"     "Carrot"
#create empty list
split_list <- list()
#fill empty list with a presence absence OTU from each location

head(allcrops_per_r_fly)
## # A tibble: 6 × 7
##   sample_ID          Ordering Region `Location and season` Day   Crop  raw_count
##   <chr>              <fct>    <chr>  <chr>                 <chr> <chr>     <dbl>
## 1 Apple_Hawkes Bay_… 32       Hawke… 104 Moteo-Pa road _2… Day_1 Apple         2
## 2 Apple_Hawkes Bay_… 33       Hawke… 104 Moteo-Pa road _2… Day_1 Apple         1
## 3 Apple_Hawkes Bay_… 34       Hawke… 104 Moteo-Pa road _2… Day_1 Apple        11
## 4 Apple_Hawkes Bay_… 54       Hawke… 104 Moteo-Pa road _2… Day_1 Apple         1
## 5 Apple_Hawkes Bay_… 34       Hawke… 104 Moteo-Pa road _2… Day_3 Apple        11
## 6 Apple_Hawkes Bay_… 14       Hawke… 185 Lawn road, Hasti… Day_1 Apple         1
#I need to fill up the NA's for the samples with 0 flies with some dummy 0 for ones species to keep them in the next stage - so this is just to add a 0 value in and I've chosed the insect '27' at random (cause i know it occurs in the dataset so Im not adding something not present)

#in '' as its a factor not a number
#filter to day 1 only
allcrops_per_r_fly_innext<-allcrops_per_r_fly %>% filter(Day=='Day_1')
allcrops_per_r_fly_innext$Ordering[is.na(allcrops_per_r_fly_innext$Ordering)] <- '27'
allcrops_per_r_fly_innext$raw_count[is.na(allcrops_per_r_fly_innext$raw_count)] <- 0

#create empty list
split_list <- list()

for (category in categories) {
  print(category)
  subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == category)
  #  subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == "Carrot")
  test<-subset_data %>% ungroup() %>% dplyr::select(sample_ID,Ordering,raw_count)
  test   <- test %>%  spread(key = sample_ID, value = raw_count)
  test<-test %>% remove_rownames %>% column_to_rownames(var="Ordering")
  test[is.na(test)] <- 0
  # test <- test[rowSums(test == 0) != ncol(test), ]
  test<-test %>% mutate(across(everything(), ~ ifelse(. > 0, 1, 0)))
  split_list[[category]]<-as(test,'matrix')
} 
## [1] "Apple"
## [1] "Pear"
## [1] "Avocado"
## [1] "Kiwifruit"
## [1] "Pak Choi"
## [1] "Radish"
## [1] "Onion"
## [1] "Carrot"
matrix_list <- list(data = list())
for (category in categories) {
  otu_table <- as(split_list[[category]], "matrix")
  matrix_list[["data"]][[category]] <- otu_table
}

#here Im setting the endpoint as NULL which is double the input sample size (this is probably most robust)
out.raw <- iNEXT3D(data = matrix_list$data, diversity = 'TD', q = c(0, 1, 2), datatype = 'incidence_raw', nboot = 50,endpoint=NULL)

#there is various ways to look at these plots:
#within a crop
ggiNEXT3D(out.raw, type = 1, facet.var = 'Assemblage') + facet_wrap(~Assemblage, nrow = 3)

#across crops
ggiNEXT3D(out.raw, type = 1, facet.var = "Order.q")
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 8 values. Consider specifying shapes manually if you need
##   that many of them.
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_point()`).

#sample completeness
ggiNEXT3D(out.raw, type = 2, facet.var = "Order.q", color.var = "Assemblage")
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 8 values. Consider specifying shapes manually if you need
##   that many of them.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

#running innext as abundance as well

#create empty list
split_list <- list()

for (category in categories) {
  print(category)
  subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == category)
  # subset_data <- allcrops_per_r_fly_innext %>% filter(Crop == "Carrot")
  test<-subset_data %>% ungroup() %>% dplyr::select(sample_ID,Ordering,raw_count)
  test   <- test %>%  spread(key = sample_ID, value = raw_count)
  test<-test %>% remove_rownames %>% column_to_rownames(var="Ordering")
  test[is.na(test)] <- 0
  test<-as.data.frame(rowSums(test))
  colnames(test) <- c(paste0(category))
  # test <- test[rowSums(test == 0) != ncol(test), ]
  # test<-test %>% mutate(across(everything(), ~ ifelse(. > 0, 1, 0)))
  split_list[[category]]<-as(test,'matrix')
} 
## [1] "Apple"
## [1] "Pear"
## [1] "Avocado"
## [1] "Kiwifruit"
## [1] "Pak Choi"
## [1] "Radish"
## [1] "Onion"
## [1] "Carrot"
matrix_list <- list(data = list())
for (category in categories) {
  otu_table <- as(split_list[[category]], "matrix")
  matrix_list[["data"]][[category]] <- otu_table
}

#here Im setting the endpoint as NULL which is double the input sample size (this is probably most robust)
out.raw <- iNEXT3D(data = matrix_list$data, diversity = 'TD', q = c(0, 1, 2), datatype = 'abundance', nboot = 50,endpoint=NULL)

#there is various ways to look at these plots:
#within a sample
ggiNEXT3D(out.raw, type = 1, facet.var = 'Assemblage') + facet_wrap(~Assemblage, nrow = 3)

#across samples
ggiNEXT3D(out.raw, type = 1, facet.var = "Order.q")
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 8 values. Consider specifying shapes manually if you need
##   that many of them.
## Warning: Removed 6 rows containing missing values or values outside the scale range
## (`geom_point()`).